How to make my python script easy portable? or how to compile into binary with all module dependencies?

Programs that can do what you ask for are:

  • PyInstaller: http://www.pyinstaller.org/ [Windows, Linux, OS X]
  • cx_freeze: http://cx-freeze.sourceforge.net/ [Windows, Linux]
  • py2exe: http://www.py2exe.org/ [Windows]
  • py2app: http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html [os x]

But as mentioned you can also create a Package with Distribute and have the other packages as dependencies. You can then uses pip to install that package, and it will install all of the packages. You still need to install Python and pip, though.


cx_freeze will append your python scripts to a standalone Python loader and produce a directory containing the program, and shared library dependencies. You can then copy the resulting distribution to other machines independent of Python or your modules.

$ cat hello.py 
print "Hello, World!"
$ ls dist/
datetime.so  _heapq.so  hello  libpython2.6.so.1.0  readline.so
$ cat hello.py 
print "Hello, World!"
$ cxfreeze hello.py 
... <snip> ...
$ ls dist/
datetime.so  _heapq.so  hello  libpython2.6.so.1.0  readline.so
$ ./dist/hello
Hello, World!

A better answer may be to create a PIP package that identifies these third modules as dependencies, so installation can be as simple as "pip install mypackage; ./package"


Python will also look for import modules in the current directory, so you don't have to install them into the python directory. Your distribution file structure might look like:

main.py
module1/
    __init__.py, ...
module2/
    __init__.py, ...

Where main.py has import module1, module2