Freeze a program created with Python's `click` pacage

后端 未结 1 1509
梦谈多话
梦谈多话 2021-02-06 02:21

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 t         


        
相关标签:
1条回答
  • 2021-02-06 03:13

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

    Starting click app when frozen

    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:])
    

    Building an exe with pyinstaller

    This simple test app can be built simply with:

    pyinstaller --onefile hello.py
    

    Test Code:

    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:])
    

    Testing:

    >dist\test.exe an_arg
    I AM WORKING (an_arg)
    
    0 讨论(0)
提交回复
热议问题