Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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.

like image 673
Jason Keene Avatar asked Jul 11 '12 16:07

Jason Keene


People also ask

How do you add dependencies in Python?

The recommended way to install Python library dependencies is with the pip command when a virtualenv is activated. Pip and virtualenv work together and have complementary responsibilities. Pip downloads and installs application dependencies from the central PyPi repository.

How do I show dependencies in Python?

The dependencies of the installed Python packages can be listed using the built-in pip show command. Alternatively the dependencies can be shown as a tree structure using the pipdeptree command.

Can Python modules have dependencies?

Dependency Hell Dependency conflicts occur when different Python packages have the same dependency, but depend on different and incompatible versions of that shared package. Because only a single version of a dependency is permitted in any project's environment, finding a compatible solution can be difficult.

How do you manage dependencies in Python?

Using venv and pipenv are two methods of managing dependencies in Python. They are simple to implement and, for most users, adequate solutions for handling multiple projects with different dependencies. However, they are not the only solutions. Other services can complement their use.


2 Answers

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/

like image 128
Simeon Avatar answered Sep 23 '22 20:09

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.

like image 30
jfs Avatar answered Sep 21 '22 20:09

jfs