< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039196038240039088173205162105045222218067026199146066159195

Distributing Python Applications

You have more than one way to pack your Python files and distribute your application. It depends only on the kind of goal that you are trying to reach. Probably your greatest concern is about how to hide the source code of your application. Well… Some suggest the use of encryption algorithms, others the distribution of Python bytecode files. All these sound like good solutions, but they have their individual problems. If you are really worried about opening your code to the public, I suggest that you convert your Python application to C, and distribute a compiled executable. Ask yourself if it is really a benefit to hide the source to your program. It is just as bad to illegally distribute a program that comes with source, and could potentially increase the value to the client.

When creating your distribution package, it is important to keep in mind the directory location where you are saving your files. Python must know where to look. Python requires module files to be available in one or more directories listed in the sys.path. To see the current Python modules search path, start Python and type:

					
import sys
print sys.path

				

You can also allow your program to find a specific module placed somewhere else on the disk. For that, you just need to add one more entry in the sys.path list. In the next example, we intend to have a module called mymodule stored in a directory called /usr/users/andre, which is not part of the sys.path yet.

					
#!usr/local/bin/python
import sys
sys.path.insert(0,"/usr/users/andre")
import mymodule
mymodule.main()

				

If you are using Python on a Windows platform, you can try the following approach to pack all files on a single structure:

  1. Create a root directory.

  2. Put the following files on this directory: python.exe, pythonw.exe, _tkinter.pyd, python15.dll, tcl80.dll, tk80.dll, and any other specific libraries that your application might need.

  3. Create three directories under the root: \ LIB, \ TCL, and \ TK, and copy all the necessary files to these subdirectories.

  4. Now, create a batch file where you set the value of the following variables: PYTHONPATH, TCL_LIBRARY, and TK_LIBRARY and create a call to your application in the file. Note that if you want to avoid opening the interpreter, you need to open your application using the pythonw application.

You can zip this entire structure and freely distribute it. The person who receives the package just has to execute the batch file to execute your application. If you want, you can change your program to use that previous technique of dynamically informing the path where your modules are located. Therefore, you don't need to configure the PYTHONPATH environment variable.

The Python distribution contains a tool that saves much time when you are dealing with distribution issues. This tool is called Freeze.

This tool is able to freeze a Python script into an executable in order to let you ship arbitrary Python programs to people who don't have Python. Note that in order to freeze programs that use Tkinter, Tcl/Tk must be installed on the target system.

Freeze works by converting all the Python code of your application to a stream of Python bytecodes that can be later executed by the Python interpreter. For each module that is opened, Freeze looks for other necessary modules too. After all modules are converted to bytecode format, Freeze glues them all, and creates a Makefile file that can be used by calling the make command. Note that the resulting executable contains all your code plus the Python interpreter and the necessary library modules. Therefore, you should expect a big file.

Freeze is a great option for the cases in which you don't want your users to see and copy your source code. Remember that the resulting file is an executable just like the ones created by regular compiled applications.

In order to use Freeze, you just need to perform a simple call, such as

					
python freeze.py hello.py

				

Freeze creates a number of files: frozen.c, config.c, and Makefile, plus one file for each Python module that gets included named M_<module>.c. To produce the frozen version of your program, you can simply type "make". This should produce a binary file. If the filename argument to Freeze was hello.py, the binary will be called hello.

Details for usage under Win32 systems can be found on your own script at your local installation.

If you built Python with some required modules as shared libraries (DLLs), the frozen program will still require these extra files. If this is a problem (and it probably is if you are considering the freeze tool), you should recompile Python (using the previous instructions) with the required modules linked into the Python executable.

Note that you are not tied to the Freeze utility. There are a couple of other options available too. Check them out.

SqueezeTool

This is a program, written by Fredrik Lundh, that is able to squeeze a Python Application and all its support modules into a single, compressed package of bytecode files. Whenever it becomes necessary, a special script is used to open the package and run the bytecode files.

http://starship.python.net/crew/fredrik/ipa/squeeze.htm

Python2C—The Python to C Translator

Python2C is a Python to C translator, written by Bill Tutt and Greg Stein, that attempts to speed up Python code by removing a significant fraction of the Python interpreter overhead.

http://lima.mudlib.org/~rassilon/p2c/

Small Python

This tool was written by Greg Stein in order to create minimal Python distributions. Note that although it is built for Windows, the concept and source code can be useful for other Python platforms, as well.

http://www.lyra.org/greg/small/

Gordon McMillan's Installer

Gordon McMillan wrote this tool by taking Fredrik's Squeeze idea and Greg Stein's Small distribution, and combining them. The result is cross-platform, small (the python Standard Library fits in a 500K archive) and fast (much less I/O for an import) distribution installer for Python.

http://www.mcmillan-inc.com/install1.html

Distutils

Python 2.0 contains a brand-new distribution package as part of its Standard Library. This package is called distutils, and is totally documented in a new set of manuals that also join the official Python documentation. This package is able to create source and binary distributions.

The logic used by this package, automatically detects the platform, recognizes the compiler, compiles the C extension modules, and installs the distribution into the proper directory.

In order to install a script using this package, you need to run a setup.py script with the install command.

						
python setup.py install

					

Note that you need to write the setup.py script in order to execute the package. This file can be very simple when you are using only .py files, such as in the next example.

						
from distutils.core import setup
setup (name = "myapp", version = "1.0", py_modules = ["bikes",
 "cars"])

					

It is important to know that you are not tied to use only .py files; you can also use packages and C extensions. Check the official documentation for more details.

The sdist command, which can be passed to python setup.py sdist, builds a source distribution such as myapp-1.0.tar.gz.

You can also add you own commands—that isn't difficult at all. Bundled with the package, there are some contributed commands already written for you, such as bdist_rpm and bdist_wininst, which create an RPM distribution and a Windows installer, respectively.


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

applications
      distributing 2nd
creating
      application distribution packages 2nd
distributing
      applications 2nd
distutils package 2nd
files
      packing 2nd
Freeze tool 2nd
Lundh, Fredrik
McMillan, Gordon
modules
      mymodule
mymodule
packages
     application distribution
            creating 2nd
      distutils 2nd
packing
      files 2nd
programs
      distributing 2nd
software
      distributing 2nd
Standard Library
      distutils package 2nd
Stein, Greg 2nd
sys.path
tools
      Freeze 2nd
Tutt, Bill
utilities
      Freeze 2nd

© 2002, O'Reilly & Associates, Inc.