views:

116

answers:

4

Is there a way to put together Python files, akin to JAR in Java? I need a way of packaging set of Python classes and functions, but unlike a standard module, I'd like it to be in one file.

+5  A: 

Take a look at Python Eggs: http://peak.telecommunity.com/DevCenter/PythonEggs

Or, you can use regular zips: http://docs.python.org/library/zipimport.html

truppo
Thank you! I had seen the term 'egg' used, but I had no clue what it was.
c4757p
+1  A: 

The simplest approach is to just use zip. A jar file in Java is a zipfile containing some metadata such as a manifest; but you don't necessarily need the metatada -- Python can import from inside a zipfile as long as you place that zipfile on sys.path, just as you would do for any directory. In the zipfile you can have the sources (.py files), but then Python will have to compile them on the fly each time a process first imports them; or you can have the bytecode files (.pyc or .pyo) but then you're limited to a specific release of Python and to either absence (for .pyc) or presence (for .pyo) of flag -O (or -OO).

As other answers indicated, there are formats such as .egg that enrich the zipfile with metatada in Python as well, like Java .jar, but whether in a particular use case that gives you extra value wrt a plain zipfile is a decision for you to make

Alex Martelli
+1  A: 

You can create zip files containing Python code and import from zip files using zipimport. A system such as PyInstaller (cross-platform) or py2exe (Windows) will do all this for you.

Vinay Sajip
A: 

Read this PEP for informations.
Also, Import modules from Zip.

Nick D