冻结使用Python的click程序创建的程序 [英] Freeze a program created with Python's `click` pacage

查看:174
本文介绍了冻结使用Python的click程序创建的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a command line program that uses Python's click package. I can install and run it locally, no problem with:

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

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

Now, I'd like to create an executable file that can be distributed and run standalone. Typically, since I'm in a Windows environment, I would use one of py2exe, pyinstaller or cx_Freeze. However, none of these packages work.

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

More specifically, they all generate an executable, but the executable does nothing. I suspect this problem is because my main.py script doesn't have a main function. Any suggestions would be very helpful, thanks in advance!

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

Can reproduce the issues with code copied from here.

hello.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 文件来创建可执行文件和任何其他所需的文件,那将不胜感激.

If someone could supply a working setup.py file to create an executable and any other required files, that would be much appreciated.

从控制台:

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

推荐答案

我更喜欢 pyinstaller 替代其他方法,因此我将根据 pyinstaller 进行回答.

I much prefer pyinstaller to the other alternatives, so I will cast an answer in terms of pyinstaller.

您可以检测到何时使用pyinstaller冻结了程序,然后像这样启动click应用程序:

You can detect when your program has been frozen with pyinstaller, and then start the click app like:

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

使用pyinstaller构建exe

这个简单的测试应用程序可以通过以下方式轻松构建:

Building an exe with pyinstaller

This simple test app can be built simply with:

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的click程序创建的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆