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

Is there any way to compile python script into binary? I have one file python script which uses a lot of modules. What I would like is to have its copy on other machines (freebsd) but without installing all needed modules on every host.

What are possible solutions in such cases?

Thanks in advance!

Answers


Programs that can do what you ask for are:

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


You probably want to create a Python package from your script. In the end you will be able to do pip install mypackage on any host and all the required modules will be downloaded and installed automatically.

See this question on how to create such a package.


I have script which imports these modules: urllib, urllib2, cookielib, BaseHTTPServer, sys, tempfile, paramiko, logging, re, OptionParser, lxml.

You probably want to create a Python package from your script. In the end you will be able to do pip install mypackage on any host and all the required modules will be downloaded and installed automatically.

  1. suppose that I have script as python package
  2. copied it to another host
  3. started pip install mypackage

As I understood it will look for modules that should be imported and will download and install dependencies.

This is not very good solution in my case. Users on other hosts(~20) should be able to run script without additional download/install procedure.

Hope that cx_freeze is what I need. Thanks for your answers.


Need Your Help

clearcase issues with default.magic file - not visible in Win7

windows-7 clearcase

I'm having some issues with my default.magic file in clearcase when adding files to clearcase.

How to prevent popups when loading a keystore

java windows x509certificate smartcard cryptoapi

Hi as corollary to this question I wanted to ask if you know how to prevent the poping of dialogue either to ask for password or to ask to insert a certificate.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.