我有一个 Python API 和一些根据该 API 构建的示例项目。我如何使用包构建它,以最终分发?
我有一个用 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。
编辑:我已经看到使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是我通常如何构建我的 python 包:
MANIFEST.in
pyproject.toml
setup.cfg
src/game/__init__.py
build ndpublish
如果您在 Windows 上并且想要
立即 构建和发布您的包,在
exemples
文件夹下,您可以执行from game.api import *
below how i usually structure my python packages:
MANIFEST.in
pyproject.toml
setup.cfg
src/game/__init__.py
build nd publish
if you are on windows and you want to build and publish your package
now, under
exemples
folder, you can dofrom game.api import *