Why py and not pyc or pyo?

When I install a python deb package, say python-numpy the files are plain python files. How do I tell to the package manager -- that I actually would rather use pyc or even better -- pyo files?


Solution 1:

You already have them and you're already using them

Well-packaged Python applications are being compiled to .pyc files in a script run after installation of the files has taken place. This is needed according to the packaging guidelines to be able to adopt to the Python installation you're using at that moment. Do remember that .pyc files are very specific to your system (Python version and dependencies).

All .pyo and .pyc files are therefore specifically excluded in packages and tagged as errors by Lintian:

Compiled python source files must not be included in the package. These files should be removed from the package and created at package installation time in the postinst.

Refer to Debian Python Policy section 2.6 (Modules Byte-Compilation) for details.

Severity: serious, Certainty: certain

In the case of python-numpy this post-install byte-compilation is handled by the debhelper hook of pycentral. After installation it looks like this:

ls -l /usr/lib/python2.7/dist-packages/numpy
lrwxrwxrwx  1 root root     47 Mar 20  2012 add_newdocs.py -> ../../../../share/pyshared/numpy/add_newdocs.py
-rw-r--r--  1 root root 251912 Aug  6 22:06 add_newdocs.pyc

Some more background information

The reason for why these .pyc/.pyo files aren't compiled at runtime during the first application startup as you expected is the following.

The Python files are installed in a system-wide directory, available for all users on the system. Whenever a user starts the application, the Python interpreter can read the .py files, but it can't write to the directories (e.g. /usr/lib/python2.7/dist-packages/). This is part of general security in all Linux systems; users shouldn't write to /usr, only with root rights this should be possible. For this reason, the APT hooks will compile the Python files for you at installation time; firstly to minimize the package size, secondly to be able to retrigger the hooks once something changes on your system regarding Python, because it's required to recompile when they get incompatible during upgrades for example.

This won't, however, affect regular user-owned python files being compiled at runtime.