我有一个 Python API 和一些根据该 API 构建的示例项目。我如何使用包构建它,以最终分发?

发布于 2025-01-12 00:32:14 字数 997 浏览 3 评论 0原文

我有一个用 Python 编写的 API,存在于两个文件中,如下面的文件结构所示(game_api.pydrift.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_apicli_gamegui_game 转换为包,但无法导入 game_api 进入两个示例项目。

例如,在 cli_game.py 中,我尝试了 from game_api import * 无济于事。

如果我不将所有示例文件放在与 game_api 相同的目录中,那么如何使内容井井有条并保存在包中?

我不确定应该如何构建它,我需要先解决这个问题,然后才能开始研究如何将其全部打包放在 github/pypi 上,以便其他人可以使用该 API。

编辑:我已经看到使用 os 文件导入重定向的类似问题的答案,这似乎有点 hacky/凌乱。当然有一个正确的方法来构建这种场景。看起来应该很简单。

I have an API written in Python that exists in two files as seen in the below file structure (game_api.py and drift.py), and two projects written utilising that API that I want to provide as examples in my project.

I am having trouble understanding how to structure these files using packages.

Current file structure:

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

When building the API and examples I had all the files in one directory and it was very messy.

I've tried to turn the folders game_api and cli_game and gui_game into packages, however I can't import game_api into the two example projects.

In cli_game.py for example I have tried from game_api import * to no avail.

If I don't put all the example files in the same directory as the game_api, then how can I keep things organised and in packages?

I'm not sure how I should structure it, and I need to get this right before I can start to work out how to package it all up to put on github/pypi so that other people can use the API.

edit: I have seen answers to similar questions that use os file import redirects, and this seems a bit hacky/messy. Surely there is a proper way to structure this kind of scenario. It seems like it should be simple enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情域 2025-01-19 00:32:14

下面是我通常如何构建我的 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)

MANIFEST.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))

[..]

build ndpublish

如果您在 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 *

below how i usually structure my python packages:

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)

MANIFEST.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))

[..]

build nd publish

if you are on windows and you want to build and publish your package

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/*

now, under exemples folder, you can do from game.api import *

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文