What is StaticPython?

StaticPython is a statically linked version of the Python 2.x (currently 2.7.1) and 3.x (currently 3.2) and Stackless Python interpreters and their standard modules for 32-bit (i686, i386, x86) Linux, Mac OS X and FreeBSD systems. It is distributed as single, statically linked 32-bit executable binaries, which contains the Python scripting engine, the interactive interpreter with command editing (readline), the Python debugger (pdb), most standard Python modules (including pure Python modules and C extensions), coroutine support using greenlet and multithreading support. The binary contains both the pure Python modules and the C extensions, so no additional .py or .so files are needed to run it. It also works in a chroot environment. The binary uses uClibc, so it supports username lookups and DNS lookups as well (without NSS).

StaticPython is open-source, and it's available on GitHub. It builds the statically linked binary from the CPython 2.7 and 3.2 sources with some small modifications.

StaticPython is similar to PyRun, except that StaticPython doesn't let you compile C extensions, and PyRun needs several libraries (e.g. glibc, OpenSSL, zlib, SQLite, bzip2) installed on the host, and StaticPython doesn't need anything (it runs even if it's the only file on the filesystem). Another important difference is that PyRun integrates nicely with packaging (setuptools, pip etc.), and is a nice, self-contained replacement for virtualenv. StaticPython doesn't help you with packaging, you have to do Python library installation and distribution manually. See this blog post for a more detailed comparison

StaticPython is similar to Portable Python, except that StaticPython runs on Linux instead of Windows, and it has Stackless Python, greenlet and coroutine-based I/O library support.

StaticPython is similar to MicroPython in the sense that both of them can be compiled to a single-file statically linked binary executable on Linux i386. MicroPython supports many more architectures (including microcontrollers), and it's a complete reimplementation of the Python 3.4 language (but contains only a fraction of the Python standard library), without reusing code from the CPython interpreter. (Static Python uses CPython.) The binary size of MicroPython is about 17.56 times smaller than StaticPython See this blog post about a file size comparison.

StaticPython is a bit similar to bbfreeze and PyInstaller, but StaticPython contains the Python interpreter, all the .py library files and all the built-in C extensions in a single executable file (while bbfreeze generates a separate .so file for each C extension library), and it doesn't contain any application-specific .py or .pyc files.

Here is how to use StaticPython:

$ curl https://raw.githubusercontent.com/pts/staticpython/master/d.sh | bash /dev/stdin python2.7-static
$
./python2.7-static
Python 2.7 (r27:82500, Aug 11 2010, 10:51:33)
[GCC 4.1.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Here is how to use the Stackless version:

$ curl https://raw.githubusercontent.com/pts/staticpython/master/d.sh | bash /dev/stdin stackless2.7-static
$
./stackless2.7-static
Python 2.7 Stackless 3.1b3 060516 (release27-maint, Aug 11 2010, 13:55:35)
[GCC 4.1.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import stackless
>>>

Here is how to use the stacklessco version, with Syncless:

$ curl https://raw.githubusercontent.com/pts/staticpython/master/d.sh | bash /dev/stdin stacklessco2.7-static
$
./stacklessco2.7-static
Python 2.7.1 Stackless 3.1b3 060516 (release27-maint, Feb  1 2011, 16:57:16)
[GCC 4.1.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from syncless import coio
>>> coio.sleep(1.5)
(sleeping for 1.5 second)
<object object at 0xf7709490>
>>>

Download for Linux

The latest release can be downloaded here (pick the one which best suits your needs):

Download for Mac OS X

These binaries have been tested on Mac OS X 10.5 and 10.6.

The latest release can be downloaded here (pick the one which best suits your needs):

Download for FreeBSD

Please use the Linux downloads above and run those binaries in Linux Binary Compatibility mode on FreeBSD. StaticPython has been tested and found working on FreeBSD 8.1. (Maybe it works with earlier FreeBSD versions as well.)

Installation for FreeBSD

As a preparation on FreeBSD 8.1, please run kldload linux as root to load the kernel module for Linux binary compatibility, and &mdash; optionally &mdash; add linux_enable="YES" to /etc/rc.conf to make this change permanent across reboots. Continue with downloading and using the Linux binaries of StaticPython.

See more about installing Linux Binary Compatibility. Please note that it's not necessary to install any Linux runtime libraries (e.g. linux_base), since the StaticPython binaries contains all the code they need.

Please note that the Linux emulation in FreeBSD is incomplete, e.g. it doesn't emulate epoll(4), so e.g. select.epoll() wouldn't work (but it would raise IOError: [Errno 38] Function not implemented). This doesn't cause any problems with the syncless or gevent modules in stacklessco, because they use libevent2, which reverts by default to poll(2) if epoll(4) is not available, and FreeBSD emulates poll(2) properly.

For what purpose is StaticPython useful?

When isn't StaticPython recommended?

How to extend, customize or recompile StaticPython?

Here is how to recompile for yourself:

$ cd /tmp
$ git clone --depth 1 https://github.com/pts/staticpython.git staticpython-build

$ cd staticpython-build
-build
$
./build.sh               # Build the non-Stackless vesion.
$
./build.sh  stackless    # Build to Stackless version.
$
./build.sh  stacklessco  # Build to Stackless + IO (stacklessco) version.

To extend and customize StaticPython, look at the build.sh script and the files in its directory. Currently no further documentation is provided on this.

Feature details

Features provided

(Please note that the version numbers below might be lower than what's actually in the newest binary.)

Executable binary layout

The following comments apply to the Linux version, but the Mac OS X version has similar properties.

Features missing

Documentation