我有一个用Python编写的API,它存在于下面的文件结构(game_api.py
和drift.py
(中的两个文件中,还有两个使用API编写的项目,我想在项目中提供这些项目作为示例。
我很难理解如何使用包来构建这些文件。
当前文件结构:
game_api/
├── __init__.py
├── game_api.py
├── drift.py
├── examples
│ ├── cli_game
│ │ ├── __init__.py
│ │ ├── cli_game.py
│ │ ├── colouring.py
│ │ └── keyboard.py
│ └── gui_game
│ └── gui_game.py
在构建API和示例时,我将所有文件都放在一个目录中,这非常混乱。
我已经尝试将文件夹game_api
、cli_game
和gui_game
转换为包,但是我无法将game_api
导入到这两个示例项目中。
例如,在cli_game.py
中,我尝试了from game_api import *
,但没有成功。
如果我不把所有的示例文件都放在game_api的同一个目录中,那么我该如何将它们组织起来并打包?
我不确定我应该如何构建它,我需要先弄清楚这一点,然后才能开始研究如何将其打包到github/pypi上,以便其他人可以使用API。
edit:我看到了使用os文件导入重定向的类似问题的答案,这似乎有点棘手。当然,有一种适当的方式来构建这种场景。它似乎应该足够简单。
下面是我通常如何构建我的python包:
game/
.. .github
.. .vscode
.. docs
.. exemples/
.. cli/
.. __init__.py
.. cli.py
.. colouring.py
.. keyboard.py
.. gui/
.. __init__.py
.. gui.py
.. src/
.. game/
.. __init__.py
.. api.py
.. drift.py
.. tests
.. venv
.. .editconfig
.. .gitignore
.. .readthedocs.yml
.. LICENSE
.. MANIFEST.in
.. pyproject.toml
.. README.rst
.. setup.cfg
.. setup.py (optional as per officiel docs)
歧管.in
# https://packaging.python.org/guides/using-manifest-in/
graft src/game
graft tests
global-exclude __pycache__
global-exclude *.py[cod]
pyproject.toml
[build-system]
# https://setuptools.readthedocs.io/en/latest/setuptools.html#setup-cfg-only-projects
requires = [
"setuptools >= 58",
"wheel"
]
build-backend = "setuptools.build_meta"
setup.cfg
# https://setuptools.readthedocs.io/en/latest/userguide/declarative_config.html
[metadata]
name = game
version = attr: game.__version__
description = [..]
long_description = file: README.rst
long_description_content_type = text/x-rst
author = [..]
author_email = [..]
# maintainer =
# maintainer_email =
license = [..]
license_file = LICENSE
# license_files = LICENSES/*
url = [..]
download_url = [..]
project_urls =
Documentation = [..]
Issue Tracker = [..]
Source Code = [..]
keywords = [..]
classifiers =
Development Status :: 1 - Planning
Framework :: Django
Framework :: Django :: 3.0
Framework :: Django :: 3.1
Framework :: Django :: 3.2
Intended Audience :: Developers
License :: OSI Approved :: BSD License
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3 :: Only
platforms = any
[options]
python_requires = >=3.8
install_requires =
[..]
packages = find:
package_dir =
= src
include_package_data = True
zip_safe = False
[options.packages.find]
where = src
[options.extras_require]
# tests =
# pytest
# pytest-cov
# code =
# flake8
src/game/__init__.py
VERSION = (0, 0, 1, 'dev0')
__version__ = '.'.join(map(str, VERSION))
[..]
构建和发布
如果你在windows上,你想构建并发布你的包
pip install build wheel
py -m build -n # don't forget '-n' flage to force using your project venv
pip install -e . # for editable mode, it will create symbol link to the package
# if everything is ok then pulish it on pypi
pip install twine
py -m twine upload dist/*
现在,在exemples
文件夹下,您可以进行from game.api import *