我正在尝试构建我的第一个 python 包并将其发布到 PyPi。我的目标是通过 CLI 提供某个功能,以便最终用户可以通过 pip 安装它pip3 install my-package
并直接从终端使用它my-package -fa first_argument -sa second_argument
。
假设我的包裹被称为fp-test-package
. 我制作了我的项目文件夹结构的模型,您可以在此处找到https://github.com/fabiopipitone/fp-test-package。
在模型中,我还插入了实际项目(tqdm)中所需的包之一作为要求,并保持完全相同的约定(外部文件夹和 github 存储库上的连字符,内部包的下划线,帮助程序和实用程序的相同位置和导入等等上)。这样,如果它适用于模型,它必须适用于真实项目。
这是项目的目录结构
fp-test-package
├── fp_test_package
│ ├── __init__.py
│ ├── fp_test_package.py
│ ├── helpers
│ │ ├── arguments_checkers.py
│ │ ├── csv_handlers.py
│ │ └── utility_functions.py
│ └── utils
│ ├── __init__.py
│ ├── CustomLogger.py
│ └── TqdmLoggingHandler.py
├── LICENSE
├── README.rst
├── requirements.txt
├── setup.py
├── docs
└── tests
这是 setup.py
from setuptools import setup, find_packages
with open("README.rst", "r") as fh:
long_description = fh.read()
setup(
name ='fp-test-package',
version ='0.0.1',
description='Simple test building a CLI tool package',
long_description=long_description,
long_description_content_type='text/x-rst',
license ='GPLv2',
packages = find_packages(),
entry_points ={
'console_scripts': [
'fp_test_package = fp_test_package.py:main'
]
},
classifiers =(
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v2",
"Operating System :: Linux",
),
keywords ='test packaging fabiopipitone',
install_requires = ['tqdm>=4.49.0'],
zip_safe = False
)
我尝试了几个关于如何构建和发布 CLI python 包的方法,然后我尝试sudo python3 setup.py install
从fp-test-package
目录内部(与 setup.py 相同的级别)。它似乎安装了包(我可以在 中找到条目fp-test-package==0.0.1
)pip3 freeze
但是如果我fp-test-package
在终端中尝试它会返回fp-test-package: command not found
,如果我尝试fp_test_package
它会返回:
Traceback (most recent call last):
File "/usr/local/bin/fp_test_package", line 11, in <module>
load_entry_point('fp-test-package==0.0.1', 'console_scripts', 'fp_test_package')()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 490, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2854, in load_entry_point
return ep.load()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2445, in load
return self.resolve()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2451, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
ModuleNotFoundError: No module named 'fp_test_package.py'
然后我去了sudo pip3 uninstall fp_test_package
,它成功地卸载了包。我删除了之前创建的build
,dist
和fp_test_package.egg-info
目录并尝试使用pip3 install -e .
(再次从内部fp-test-package
)。它创建了fp_test_package.egg-info
目录,但再次fp_test_package
在控制台中运行它返回
Traceback (most recent call last):
File "/usr/local/bin/fp_test_package", line 11, in <module>
load_entry_point('fp-test-package==0.0.1', 'console_scripts', 'fp_test_package')()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 490, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2854, in load_entry_point
return ep.load()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2445, in load
return self.resolve()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2451, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
ModuleNotFoundError: No module named 'fp_test_package.py'
现在,我错过了什么?我想简单地打包并安装它,以便当用户键入fp-test-package
或fp_test_package
返回有关所需--export_path
参数的错误(意味着脚本已正确启动)时,就像我从控制台调用它时一样:
$ python3 fp_test_package/fp_test_package.py
usage: fp_test_package.py [-h] -ep EXPORT_PATH [-sa SECOND_ARGUMENT] [-ta THIRD_ARGUMENT]
fp_test_package.py: error: the following arguments are required: -ep/--export_path
我该怎么做?
编辑:我注意到@Dustin 然后指出了关于 setup.py 的内容。事实上,我的一位同事 PRed 虚拟回购改变了console_scripts
部分。现在,在我同事的机器上,一切似乎都按预期工作。在我的情况下,在提取 repo 并重新运行后python3 setup.py install
,从控制台调用时fp_test_package
,它返回以下内容:
Traceback (most recent call last):
File "/usr/local/bin/fp_test_package", line 11, in <module>
load_entry_point('fp-test-package==0.0.1', 'console_scripts', 'fp_test_package')()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 490, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2854, in load_entry_point
return ep.load()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2445, in load
return self.resolve()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2451, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/usr/local/bin/fp_test_package.py", line 4, in <module>
__import__('pkg_resources').run_script('fp-test-package==0.0.1', 'fp_test_package.py')
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 667, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1452, in run_script
raise ResolutionError(
pkg_resources.ResolutionError: Script 'scripts/fp_test_package.py' not found in metadata at '/usr/local/lib/python3.8/dist-packages/fp_test_package-0.0.1-py3.8.egg/EGG-INFO'
有任何想法吗?