How to build a single python file from multiple scripts?

前端 未结 8 1706
终归单人心
终归单人心 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:21

    Use stickytape module

    stickytape scripts/blah --add-python-path . > /tmp/blah-standalone
    

    This will result with a functioning script, but not necessarily human-readable.

    0 讨论(0)
  • 2020-12-24 02:22

    If you want to package your script with all its dependencies into a single file (it won't be a .py file) you should look into virtualenv. This is a tool that lets you build a sandbox environment to install Python packages into, and manages all the PATH, PYTHONPATH, and LD_LIBRARY_PATH issues to make sure that the sandbox is completely self-contained.

    If you start with a virgin Python with no additional libraries installed, then easy_install your dependencies into the virtual environment, you will end up with a built project in the virtualenv that requires only Python to run.

    The sandbox is a directory tree, not a single file, but for distribution you can tar/zip it. I have never tried distributing the env so there may be path dependencies, I'm not sure.

    You may need to, instead, distribute a build script that builds out a virtual environment on the target machine. zc.buildout is a tool that helps automate that process, sort of like a "make install" that is tightly integrated with the Python package system and PyPI.

    0 讨论(0)
提交回复
热议问题