How to build a single python file from multiple scripts?

前端 未结 8 1773
终归单人心
终归单人心 2020-12-24 01:30

I have a simple python script, which imports various other modules I\'ve written (and so on). Due to my environment, my PYTHONPATH is quite long. I\'m also using Python 2.

8条回答
  •  误落风尘
    2020-12-24 02:07

    I found this useful:

    http://blog.ablepear.com/2012/10/bundling-python-files-into-stand-alone.html

    In short, you can .zip your modules and include a __main__.py file inside, which will enable you to run it like so:

    python3 app.zip
    

    Since my app is small I made a link from my main script to __main__.py.

    Addendum:

    You can also make the zip self-executable on UNIX-like systems by adding a single line at the top of the file. This may be important for scripts using Python3.

    echo '#!/usr/bin/env python3' | cat - app.zip > app
    chmod a+x app
    

    Which can now be executed without specifying python

    ./app
    

提交回复
热议问题