18

I want to create a private, self-contained Python environment that doesn't link back to system libraries (the way virtualenv does) and also includes its own versions of standard C/C++ libraries for Python packages that are simply wrappers for these libraries (PIL and libjpeg, for example).

I've seen commercial apps that distribute an entire runtime in a directory, so I know it can be done.

The question is, is there a tool that makes this easy to do? Virtualenv almost gets me there, esp. with the --no-site-packages and the --relocatable flags, but it doesn't solve the problem for standard "C" libraries. I want to be able to build a PIL, for example, that uses the bundled private version of libjpeg and libpng

Any suggestions?

  • 1
    Is this for your own use or do you want to package it for distribution? It's a bit unclear. – Lennart Regebro Jan 07 '11 at 07:17
  • For my own use. Ideally, I'd like a "portable environment" I can move from machine to machine of the same basic architecture and os –  Jan 07 '11 at 16:50

3 Answers3

6

It sound like what your really after is a way to bundle up a particular python installation and script into a distributable package. You can do the following:

  • Download, compile and install python into a project directory (ie. python-toolchain)
  • Create a prefix script (toolchain) that will take a relative path and update PATH, PYTHONPATH, LD_LIBRARY_PATH dynamically.
#!/bin/sh

# If your clever you can determine install directory dynamically
prefix='install directory'
toolchain=$prefix/python-toolchain
export PATH=$toolchain/bin:$prefix/app/bin:$PATH
export LD_LIBRARY_PATH=$toolchain/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$prefix/app/lib:$PYTHONPATH
exec $*
  • The magic cookie in your app script can then look like this:
#!.../install directory/toolchain python

print "Hello World!"
  • You should now be able to distribute a directory like this:
    • package/
      • python-toolchain/
      • app/
dietbuddha
  • 8,556
  • 1
  • 30
  • 34
1

Try cx_Freeze or py2exe. They're used to package Python apps to make them redistributable, and include all the packages/libraries that it depends on.

maranas
  • 1,396
  • 1
  • 12
  • 21
  • I was looking more for a way to have a portable development environment I could install on every machine I used, not a frozen system for distribution. But maybe these packaging programs can be used to freeze a dev environment, too.... –  Jan 07 '11 at 16:52
  • 1
    I see. In that case, you can just try using setuptools to create a source distribution, with install_requires defined so it will get all the dependencies you need from pypi. After that, creating a development environment on another machine would simply be just downloading the sources into that machine and running the setup script. – maranas Jan 10 '11 at 07:00
1

I've used py2exe successfully. There's also cx_Freeze.

See also here for more suggestions:

Regarding this statement:

that doesn't link back to system libraries

You of course can't completely eliminate that it requires some system libraries. If you want a Windows application it will have to use the Windows API for example. I assume you don't want to bundle an entire copy of Windows with your application just in case they don't have Windows installed....

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 1
    Right. Things that are generally included with the OS. But special libraries used by a particular package –  Jan 07 '11 at 16:51