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

Question:

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 they have 2.6/2.7 installed. I don’t want to have the user install dependencies site-wide as there might be conflicts with existing packages/versions, nor do I want them to muck around with virtualenv. I was wondering if there was a way to embed a package like dnspython inside the script (gzip/base64) and have that script access that package at runtime. Perhaps unpack it into a dir in /tmp and add that to sys.path? I’m not concerned about startup overhead, I just want a single .py w/ all dependencies included that I can distribute.

Also, there would be no C dependencies to build, only pure python packages.

Edit: The script doesn’t have to be a .py. Just so long as it is a single executable file.

Asked By: Jason Keene

||

Answers:

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
Answered By: Phil Cooper

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/

Answered By: Simeon

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.

Answered By: jfs
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.