Python 单击 CLI 应用程序
当您使用 Click 库构建 Python CLI 应用程序时,您可以执行以下操作:
@click.version_option()
def cli():
'''
Main Entry Point to Click Interface
'''
能够做到这一点:
[user@host]$ clickapp --version
点击打包在pex
但是当我将它打包为一个pex
文件时,我的 click 应用程序的所有其他参数、选项、命令、子命令都可以工作,除了--version
.
当我运行时(clickapp 现在是一个二进制可执行pex
文件):
[user@host]$ ./clickapp --version
我收到以下错误:
Traceback (most recent call last):
File "/~/clickapp/.bootstrap/pex/pex.py", line 446, in execute
File "/~/clickapp/.bootstrap/pex/pex.py", line 378, in _wrap_coverage
File "/~/clickapp/.bootstrap/pex/pex.py", line 409, in _wrap_profiling
File "/~/clickapp/.bootstrap/pex/pex.py", line 508, in _execute
File "/~/clickapp/.bootstrap/pex/pex.py", line 610, in execute_entry
File "/~/clickapp/.bootstrap/pex/pex.py", line 626, in execute_pkg_resources
File "/~/.pex/installed_wheels/7bcb1b5bf49ca3f89c348c338d33c04e3d59dfc1/click-7.1.2-py2.py3-none-any.whl/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/~/.pex/installed_wheels/7bcb1b5bf49ca3f89c348c338d33c04e3d59dfc1/click-7.1.2-py2.py3-none-any.whl/click/core.py", line 781, in main
with self.make_context(prog_name, args, **extra) as ctx:
File "/~/.pex/installed_wheels/7bcb1b5bf49ca3f89c348c338d33c04e3d59dfc1/click-7.1.2-py2.py3-none-any.whl/click/core.py", line 700, in make_context
self.parse_args(ctx, args)
File "/~/.pex/installed_wheels/7bcb1b5bf49ca3f89c348c338d33c04e3d59dfc1/click-7.1.2-py2.py3-none-any.whl/click/core.py", line 1212, in parse_args
rest = Command.parse_args(self, ctx, args)
File "/~/.pex/installed_wheels/7bcb1b5bf49ca3f89c348c338d33c04e3d59dfc1/click-7.1.2-py2.py3-none-any.whl/click/core.py", line 1048, in parse_args
value, args = param.handle_parse_result(ctx, opts, args)
File "/~/.pex/installed_wheels/7bcb1b5bf49ca3f89c348c338d33c04e3d59dfc1/click-7.1.2-py2.py3-none-any.whl/click/core.py", line 1630, in handle_parse_result
value = invoke_param_callback(self.callback, ctx, self, value)
File "/~/.pex/installed_wheels/7bcb1b5bf49ca3f89c348c338d33c04e3d59dfc1/click-7.1.2-py2.py3-none-any.whl/click/core.py", line 123, in invoke_param_callback
return callback(ctx, param, value)
File "/~/.pex/installed_wheels/7bcb1b5bf49ca3f89c348c338d33c04e3d59dfc1/click-7.1.2-py2.py3-none-any.whl/click/decorators.py", line 295, in callback
raise RuntimeError("Could not determine version")
RuntimeError: Could not determine version
细节
setup.py
文件:
from setuptools import setup, find_namespace_packages
setup(
name='clickapp',
version='0.1.3',
author='Hamza M Zubair',
packages=find_namespace_packages(),
include_package_data=True,
package_data={
'': ['*.yaml'],
},
classifiers=[
'Programming Language :: Python :: 3',
'Operating System :: OS Independent',
'Natural Language :: English',
'License :: Other/Proprietary License',
],
python_requires='>=3.6',
install_requires=[
'click',
'pandas',
'sqlalchemy',
'jinjasql',
'pyyaml',
'joblib',
'python-dateutil',
'loguru',
'pymysql',
'xgboost',
'sklearn',
'wheel',
'importlib-resources'
],
entry_points='''
[console_scripts]
clickapp=clickapp.cli:cli
''',
)
用于创建pex
文件的命令:
[user@host]$ python setup.py bdist_pex --bdist-all
工具规格
我正在使用以下版本的库/包在不同的系统中构建和运行 pex 文件。目标机器只有 Python 而没有库,因为 pex 文件不需要库/virtualenv 等。
Build Machine OS: CentOS Linux release 7.8.2003 (Core)
Build Machine Python: 3.6.8
setuptools: 51.0.0
pex: 2.1.21
click: 7.1.2
Target Machine OS: CentOS Linus release 7.4.1708 (Core)
Target Machine Python: 3.6.8
我试过的
- 我已经测试了我的全部功能
clickapp
,并且所有其他参数和命令都可以完美运行。
即使这样也能正确显示我的 clickapp 的帮助。
[user@host]$ ./clickapp --help
- 我尝试重新构建包几次
- 我只在 Python3.6 中测试过这个。我没有尝试过不同的python版本,在源系统和目标系统中都设置它有点困难。
- 当我删除时,
@click.version_option()
我收到错误:--version not implemented
,正如预期的那样 - 我尚未在第二个目标系统上对其进行测试,以防我当前目标服务器的某些特性导致错误
更多信息
为了帮助 SO 用户,我还应该提供哪些其他信息?