Search code examples
pythondistutils

Using distutils to make a package sutiable for zipimport


I'm embedding python in an application that I'm creating and I'm looking for a convenient way to distribute the python code with it. I recently read about zipimport and figured that would be a convenient way to distribute all my python code rather than including the fully expanded tree. So, I set up my package similarly to this:

.
├── setup.py
└── testpack
    ├── __init__.py
    └── pack1
        ├── foo.py
        └── __init__.py

where setup.py looks like:

from distutils.core import setup

setup(
    name='testpack',
    author='FatalError',
    version='1.0',
    packages=['testpack.pack1']
)

and then I ran python setup.py bdist --format=zip. But then when I look at the content of the zip:

$ unzip -l testpack-1.0.linux-x86_64.zip 
Archive:  testpack-1.0.linux-x86_64.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      183  2013-03-13 10:47   usr/local/lib/python2.7/dist-packages/testpack-1.0.egg-info
      152  2013-03-13 10:47   usr/local/lib/python2.7/dist-packages/testpack/pack1/__init__.pyc
      181  2013-03-13 10:47   usr/local/lib/python2.7/dist-packages/testpack/pack1/foo.pyc
        0  2013-03-13 10:41   usr/local/lib/python2.7/dist-packages/testpack/pack1/__init__.py
       33  2013-03-13 10:41   usr/local/lib/python2.7/dist-packages/testpack/pack1/foo.py
---------                     -------
      549                     5 files

Clearly the result is (not so surprisingly) not suitable for use with zipimport. Rather, this is a zip archive meant to be unzipped at / to install the package.

Is there any way to get distutils (or setuptools, etc) to instead build the package so that it will work with zipimport? Since the code I have is really application specific, it doesn't belong installed into the system library.

I realize that I can script it myself, but well, then what fun is that? For example, I want to make sure everything is pre-compiled and my package includes a unit test package that isn't meant to be distributed. So, I was hoping one of these tools could do it for me.


Solution

  • egg archives are zipimport-capable (unless flagged to the contrary, in which case they'll be unpacked during installation).

    Use python setup.py bdist --formats=egg.