gpt4 book ai didi

python - 卡住使用 Python `click` 包创建的程序

转载 作者:太空狗 更新时间:2023-10-29 21:48:33 26 4
gpt4 key购买 nike

我有一个使用 Python 的 click 的命令行程序包裹。我可以在本地安装和运行它,没问题:

pip install --editable . # (or leave out the editable of course)

现在,我想创建一个可以分发和独立运行的可执行文件。通常,由于我在 Windows 环境中,我会使用 py2exepyinstallercx_Freeze 之一。但是,这些软件包都不起作用。

更具体地说,它们都生成可执行文件,但可执行文件什么也不做。我怀疑这个问题是因为我的 main.py 脚本没有 main 函数。任何建议都会非常有帮助,在此先感谢!

可以使用从 here 复制的代码重现问题.

你好.py

import click

@click.command()
def cli():
click.echo("I AM WORKING")

setup.py

from distutils.core import setup
import py2exe

setup(
name="hello",
version="0.1",
py_modules=['hello'],
install_requires=[
'Click'
],
entry_points="""
[console_scripts]
hello=hello:cli
""",
console=['hello.py']
)

如果有人可以提供有效的 setup.py 文件来创建可执行文件和任何其他所需文件,我们将不胜感激。

从控制台:

python setup.py py2exe
# A bunch of info, no errors
cd dist
hello.exe
# no output, should output "I AM WORKING"

最佳答案

我更喜欢 pyinstaller到其他选择,所以我会根据 pyinstaller 来回答.

卡住时启动点击应用

您可以使用 pyinstaller 检测您的程序何时被卡住,然后像这样启动 click 应用程序:

if getattr(sys, 'frozen', False):
cli(sys.argv[1:])

用pyinstaller构建exe

这个简单的测试应用可以简单地构建:

pyinstaller --onefile hello.py

测试代码:

import sys
import click

@click.command()
@click.argument('arg')
def cli(arg):
click.echo("I AM WORKING (%s)" % arg)

if getattr(sys, 'frozen', False):
cli(sys.argv[1:])

测试:

>dist\test.exe an_arg
I AM WORKING (an_arg)

关于python - 卡住使用 Python `click` 包创建的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45090083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com