我正在尝试为我的 python 项目创建一个 cli 入口点配置。该项目具有以下结构:
/my_repo
/my_project
__init__.py (empty file)
main.py
menu.py
database.py
user.py
utility.py
/tests
LICENCE
pyproject.toml
README.md
requirements.txt
setup.cfg
setup.py
这是setup.py:
from setuptools import setup, find_packages
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
with open("requirements.txt", "r", encoding="utf-8") as fh:
requirements = fh.read()
setup(
name='My_project',
version='0.0.1',
author='John Doe',
author_email='[email protected]',
license='MIT',
description='CLI application.',
long_description=long_description,
long_description_content_type="text/markdown"
install_requires=[requirements],
python_requires='>=3.10',
packages=find_packages(),
classifiers=[
"Programming Language :: Python :: 3.10",
"Operating System :: OS Independent",
],
entry_points={
'console_scripts': [
'my_project=my_project.main:main'
]
},
)
这是main.py:
from menu import menu
def main():
menu()
if __name__ == "__main__":
main()
这是setup.cfg:
[metadata]
name = my_project
version = 0.0.1
[options]
packages = my_project
[options.entry_points]
console_scripts =
my_project = my_project.main:main
这是 pyproject.toml:
[build-system]
build-backend = "setuptools.build_meta"
requires = ["setuptools", "wheel"]
创建 setup.py、setup.cfg 和 pyproject.toml 后,我使用以下命令安装了构建
pip install build
,之后我执行了此命令 python -m build
,在构建结束时我收到以下消息:“成功构建了 My_project-0.0.1.tar.gz 和 My_project-0.0.1-py3-none-any.whl”。
此时我尝试通过执行以下命令来使用 pip 安装轮子:
pip install dist/My_project-0.0.1-py3-none-any.whl
。安装进行得很顺利,但是当我尝试执行命令来启动程序时,出现错误:
命令:
my_project
错误:
Traceback (most recent call last):
File "/Users/johndoe/development/envs/my_repo/3.10/bin/my_project", line 5, in <module>
from my_project.main import main
File "Users/johndoe/development/envs/my_repo/3.10/lib/python3.10/site-packages/my_project/main.py", line 1, in <module>
from menu import menu
ModuleNotFoundError: No module named 'menu'
有人可以告诉我我做错了什么吗?这是我第一次尝试为项目创建 CLI 入口点配置(这是我的第一个 python 项目)。 希望我提供的信息足以理解问题。 预先感谢!
将入口点移至 pyproject.toml:
[project.scripts]
my_project = "main:main"
背景:
我遇到了这个问题:
Traceback (most recent call last):
File "/home/.../ci-checkout/venv/bin/ci-checkout", line 5, in <module>
from src.start import main
ModuleNotFoundError: No module named 'src'
文件结构与您的相同,只有“src”作为源的目录名。 构建和安装成功,pyproject 有入口点:
[project.scripts]
ci-checkout = "src.start:main"
通过删除“src”解决。来自它:
[project.scripts]
ci-checkout = "start:main"
“ci-checkout”cli 之后工作正常