3

我正在尝试构建我的第一个 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 installfp-test-package目录内部(与 setup.py 相同的级别)。它似乎安装了包(我可以在 中找到条目fp-test-package==0.0.1pip3 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,distfp_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-packagefp_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'

有任何想法吗?

4

2 回答 2

1

setup.py有:

  entry_points ={ 
    'console_scripts': [ 
      'fp_test_package = fp_test_package.py:main'
    ] 
  }, 

这等效于以下导入:

from fp_test_package.py import main as fp_test_package

问题是您没有fp_test_package.py模块,您有一个包含子模块(模块名称不包含扩展名)的fp_test_package模块(带有文件的目录)。__init__.pyfp_test_package

假设您的fp_test_package.py文件定义了一个main函数,您可以将其更改为:

  entry_points ={ 
    'console_scripts': [ 
      'fp_test_package = fp_test_package.fp_test_package:main'
    ] 
  }, 

或者你可以将你的main函数移动到fp_test_package/__init__.py,所以它可能是:

  entry_points ={ 
    'console_scripts': [ 
      'fp_test_package = fp_test_package:main'
    ] 
  }, 
于 2020-10-06T17:40:53.927 回答
-1

好的,我最终在我的具体案例中发现了问题。安装时是什么导致了那个神秘的错误python3 setup.py install

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'

或安装时的等效错误pip3 install .

pkg_resources.ResolutionError: Script 'scripts/fp_test_package.py' not found in metadata at '/home/fabio/Desktop/test_python_package/fp-test-package/fp_test_package-0.0.1-py3.8.egg/EGG-INFO'

是在 $PATH 变量中缺少正确启动脚本所需的路径。

总结一下,我所做的是:

  • 根据达斯汀setup.py关于console_scripts
  • 从文件夹内部构建和安装正在运行的包pip3 install .(或者pip3 install -e .如果您想要可编辑的版本) 。fp-test-package
  • 检查脚本的二进制文件是在哪里创建的which fp_test_package(在我的例子中/home/fabio/.local/bin/fp_test_package
  • 将其添加path到 $PATH env var(在我的.bashrc- 并且.zshrc因为我使用zsh- 我添加了一行export PATH="$HOME/.local/bin:$PATH"

现在一切都按预期工作。我不会删除repo 的正确版本,所以如果有人想使用工作框架来启动项目,可以在那里找到它。

于 2020-10-07T11:34:22.430 回答