Is there a way to embed dependencies within a python script?

后端 未结 3 729
失恋的感觉
失恋的感觉 2020-12-24 07:10

I have a simple script that has a dependency on dnspython for parsing zone files. I would like to distribute this script as a single .py that users can run just so long as

相关标签:
3条回答
  • 2020-12-24 07:21

    You can package multiple Python files up into a .egg. Egg files are essentially just zip archives with well defined metadata - look at the setuptools documentation to see how to do this. Per the docs you can make egg files directly executable by specifying the entry point. This would give you a single executable file that can contain your code + any other dependencies.

    EDIT: Nowadays I would recommend building a pex to do this. pex is basically an executable zip file with non stdlib dependencies. It doesn't contain a python distribution (like py2app/py2exe) but holds everything else and can be built with a single command line invocation. https://pex.readthedocs.org/en/latest/

    0 讨论(0)
  • 2020-12-24 07:28

    please don't do this. If you do DO NOT make a habit of it.

    • pydns is BDS licensed but if you try to "embed" a gpl module in this way you could get in trouble
    • you can learn to use setuptools and you will be much happier in the long run
    • setuptools will handle the install of dependencies you identified (I'm not sure if the pydns you are using is pure python so you might create problems for your users if you try to add it yourself without knowing their environment)
    • you can set a url or pypi so that people could upgrade your script with easy_install -U
    0 讨论(0)
  • 2020-12-24 07:36

    The simplest way is just to put your python script named __main__.py with pure Python dependencies in a zip archive, example.

    Otherwise PyInstaller could be used to produce a stand-alone executable.

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