我正在制作一个 python 模板,它应该能够以 3 种不同的方式运行:
python python3-template.py
(只需导入基于目录的包python3_template
并运行python3_template.run()
).exe
构建一个 pyinstaller python3-template.py
并运行它python3-template = "python3_template:run"
并运行它我的
1
和 2
工作完美,并且 3
的轮子包含所有包含的数据。但是,安装轮子并尝试运行 python3-template
后,我收到此错误:
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:\Users\Ella\miniconda3\envs\build\Scripts\python3-template.exe\__main__.py", line 4, in <module>
ModuleNotFoundError: No module named 'python3_template'
此外,我无法在交互式解释器中
import python3_template
,出现同样的错误。当我在主项目目录中时,我可以导入该包(当然,它是从 python3_project
目录加载的),但我无法将其作为常规安装的包导入。
目录结构如下:
python3-template/
├── docs/
│ └── example.jpg
├── python3_template/
│ ├── constants/
│ │ ├── __init__.py
│ │ ├── defaults.py
│ │ ├── enums.py
│ │ ├── links.py
│ │ ├── paths.py
│ │ ├── platform.py
│ │ ├── resources.py
│ │ └── version.py
│ ├── helpers/
│ │ ├── __init__.py
│ │ └── core.py
│ ├── resources/
│ │ ├── icon.ico
│ │ └── icon.png
│ ├── __init__.py
│ ├── calculator.py
│ ├── main.py
│ └── version.yml
├── build.bat
├── environment.yml
├── LICENSE
├── MANIFEST.in
├── pyproject.toml
├── python3-template.py
├── README.md
└── README_pypi.md
这是
pyproject.toml
:
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[project]
name = "python3-template"
version = "0.1.0"
description = "A Python 3 Template"
readme = "README_pypi.md"
license= {file = "LICENSE"}
authors = [
{name = "Ella Jameson", email = "[email protected]"}
]
classifiers = []
dependencies = [
"pyyaml"
]
[project.urls]
homepage = "https://github.com/nimaid/python3-template"
repository = "https://github.com/nimaid/python3-template"
issues = "https://github.com/nimaid/python3-template/issues"
[project.scripts]
python3-template = "python3_template:run"
[tool.setuptools]
include-package-data = true
[tool.setuptools.packages.find]
where = ["python3_template"]
我什至创建了我的存储库的一个分支,如果需要,您可以使用它:https://github.com/nimaid/python3-template/tree/init-but-can't-run-script-导入错误
python3_template.main:run
(无效果)setuptools
将其覆盖回破折号)我不知道为什么这不适用于此模板。我过去已经发布过一些软件包,我很确定它们的结构是相同的,但我在将这些模式转移到这个模板时可能犯了一个错误。对于为什么它不起作用我没有其他想法,并且非常感谢您提供的任何帮助!谢谢!
我解决了!安装工具没有自动发现包,因为我的目录不完全是标准格式。从这里:
When both py-modules and packages are left unspecified, setuptools will attempt to perform Automatic discovery, which should cover most popular project directory organization techniques, such as the src-layout and the flat-layout.
src-layout
,并将我的pyproject.toml
更改为:
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[project]
name = "python3-template"
version = "0.1.0"
description = "A Python 3 Template"
readme = "README_pypi.md"
license= {file = "LICENSE"}
authors = [
{name = "Ella Jameson", email = "[email protected]"}
]
classifiers = []
dependencies = [
"pyyaml"
]
[project.urls]
homepage = "https://github.com/nimaid/python3-template"
repository = "https://github.com/nimaid/python3-template"
issues = "https://github.com/nimaid/python3-template/issues"
[project.scripts]
python3-template = "python3_template:run"
[tool.setuptools]
include-package-data = true
[tool.setuptools.packages.find]
where = ["src"]
更新我的
MANIFEST.in
、build.bat
以及引用项目目录的其他一些文件后,我可以以所有 3 种所需的形式运行我的项目!
希望这对将来的人有帮助!