英文:
PyPI - allow importing a module using a different name
问题
我最近创建了一个简单的Python包/库,并将其上传到PyPI。一切都没问题 - 我可以将我的项目上传到PyPI并使用pip重新下载,但我无法导入它,因为它包含一个句点。
我的包叫做dcode.py
(显然会导致导入问题),我希望能够只输入import dcode
(而不是.py
)来导入它。我已经寻找了解决方案,但似乎找不到 - 我希望能够在别名下导入它(dcode
)而不是dcode.py
(显然不起作用)。
有办法可以做到这一点吗?我认为必须有(例如,包括discord.py
和py-cord
两个包都可以只使用import discord
来导入)。有人知道如何做到这一点吗?
以防万一您需要它,我的pyproject.toml
文件如下:
[project]
name = "dcode.py"
version = "0.0.2"
authors = [
{ name = "Dylan Rogers", email = "opendylan@proton.me" },
]
description = "dcode - the Python package for everything"
readme = "README.md"
requires-python = ">=2.0"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
[project.urls]
"Homepage" = "https://github.com/dylanopen/dcode.py"
"Bug Tracker" = "https://github.com/dylanopen/dcode.py/issues"
我尝试导入dcode.py
:
import dcode.py
但我只得到这个错误:
File "/run/media/dylan/Programming/dcode.py-test/test.py", line 1, in <module>
import dcode_py
ModuleNotFoundError: No module named 'dcode_py'
如果有帮助的话,我正在使用Manjaro Linux(不知道为什么这会有帮助,我只是需要知道如何让用户通过输入import dcode
来导入dcode.py
,就像他们导入discord.py
一样)
非常感谢任何帮助!
英文:
I recently created a simple python package / library and uploaded it to (PyPI). That's all fine - I can upload my project to pypi and download it again using pip, but I can't import it because it contains a full stop .
My package is called dcode.py
(which obviously causes issues with importing), I'd like to be able to import it by just typing import dcode
(without the .py
). I've looked around for a solution but can't seem to find one - I'd like to be able to import it under an alias (dcode
) instead of dcode.py
(which obviously doesn't work).
Is there a way to do this? I'd assume there must be (for example, both the packages discord.py
and py-cord
can both just be imported using import discord
. Does anyone know how to do this?
Just in case you need it, my pyproject.toml
file looks like this:
[project]
name = "dcode.py"
version = "0.0.2"
authors = [
{ name = "Dylan Rogers", email = "opendylan@proton.me" },
]
description = "dcode - the Python package for everything"
readme = "README.md"
requires-python = ">=2.0"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
[project.urls]
"Homepage" = "https://github.com/dylanopen/dcode.py"
"Bug Tracker" = "https://github.com/dylanopen/dcode.py/issues"
And my entire project is on github.
Here is the PyPI project
I've tried importing dcode.py
:
import dcode.py
But I just get this error:
File "/run/media/dylan/Programming/dcode.py-test/test.py", line 1, in <module>
import dcode_py
ModuleNotFoundError: No module named 'dcode_py'
If it helps, I'm using Manjaro Linux (no idea why it would help though, I just need to know how to allow users to import dcode.py
by typing import dcode
, like they import discord.py
as discord
)
Thank you so much for any help!
答案1
得分: 3
这是您的项目应该具有的推荐目录结构:
dcode.py/
├── LICENSE
├── pyproject.toml
├── README.md
├── src/
│ └── dcode/
│ ├── __init__.py
│ └── io/
│ ├── __init__.py
│ └── file.py
└── tests/
这里的dcode
(如dcode.py/src/dcode
中定义)是唯一可导入的顶级包的名称。它还包含一个子包dcode.io
(如dcode.py/src/dcode/io
中定义)。
在pyproject.toml
文件中,实际发布到_PyPI_并通过pip安装的项目名称在包元数据中定义:
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[project]
name = "dcode.py"
version = "0.0.1"
authors = [
{ name = "Dylan Rogers", email = "opendylan@proton.me" },
]
description = "dcode - the Python package for everything"
readme = "README.md"
requires-python = ">=3.8"
[project.urls]
"Homepage" = "https://github.com/dylanopen/dcode.py"
"Bug Tracker" = "https://github.com/dylanopen/dcode.py/issues"
[tool.setuptools.packages.find]
where = ["src"]
不要将包含构建的sdist和wheel分发包构件的dist
目录推送到源代码存储库中。
不要将任何*.egg-info
目录推送到源代码存储库中。
将所有实际的Python代码放在src
目录中。
项目的名称可以包含点,如dcode.py
,尽管我建议避免这样做。
实际的顶级导入包和模块的名称可以完全不同于发行项目的名称。
如果您在pyproject.toml
中的包元数据中声明了许可证的分类器,那么它应该与源代码存储库中的实际许可证文件匹配(您有一个GNU GPL v3.0许可证文件,但是MIT许可证分类器)。
参考资料:
- https://packaging.python.org/en/latest/tutorials/packaging-projects/
- https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html
- https://sinoroc.gitlab.io/kb/python/packaging.html
英文:
This is the recommended directory structure your project should have:
dcode.py/
├── LICENSE
├── pyproject.toml
├── README.md
├── src/
│ └── dcode/
│ ├── __init__.py
│ └── io/
│ ├── __init__.py
│ └── file.py
└── tests/
Here dcode
(as defined by dcode.py/src/dcode
) is the name of the only top-level importable package. And it also contains a sub-package dcode.io
(as defined by dcode.py/src/dcode/io
).
The name of the actual distribution project as seen on PyPI and that gets pip-installed is defined in the package metadata, in the pyproject.toml
file:
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[project]
name = "dcode.py"
version = "0.0.1"
authors = [
{ name = "Dylan Rogers", email = "opendylan@proton.me" },
]
description = "dcode - the Python package for everything"
readme = "README.md"
requires-python = ">=3.8"
[project.urls]
"Homepage" = "https://github.com/dylanopen/dcode.py"
"Bug Tracker" = "https://github.com/dylanopen/dcode.py/issues"
[tool.setuptools.packages.find]
where = ["src"]
Do not push the dist
directory containing the built sdist and wheel distribution packages artifacts into the source code repository.
Do not push any *.egg-info
directory into the source code repository.
Place all the actual Python code inside a src
directory.
The name of the project can contain a dot such as dcode.py
, although I would recommend against it.
The name of the actual top-level import packages and modules can be completely different than the name of the distribution project.
If you declare a classifier for the license in the package metadata in pyproject.toml
, then it should match the actual license file in the source code repository (you had a GNU GPL v3.0 license file but a MIT license classifier).
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论