How to compile Python scripts


Contents:

General information

Some basics about compiling Python scripts.

Usually, there is no need to manually compile, because every time you run a Python script, each of the imported modules will be compiled to byte-code automatically by default.

These files have the suffix pyc and Python runs a compiled file if it finds one with the same base name as the imported module.

A compiled Python script does not run any faster, but has a faster start-up time. This may be useful when you need to start it multiple times consecutively, e. g. in a script or when automatically reacting on file system events.

Also notice that the files which have been compiled that way still need the Python interpreter to run.


As already mentioned, there usually is no need to manually compile. This how-to is meant for advanced users, who want or need to compile the scripts for some reason.


Compile Python scripts

How to compile Python scripts using the Python shell.

First of all, you need recursive write permissions to the directory where the script files are located, because the compiled files will be created there. Without these permissions the script files can be executed, but not compiled.

As already mentioned above, all modules imported by a script will be compiled to byte-code by default, but it would be quite laborious to manually run every script of the project to ensure that all modules will be compiled. Furthermore, this precompile process can be easily done using the Python shell.

When successfully finished, there will be a compiled pyc file for each Python script inside the directory as well as all of its sub-directories. You can delete the original script files, but be aware of the fact that a compiled Python script cannot be modified anymore.

Unix-like systems

So, to recursively compile all Python scripts inside the directory /opt/foo, open a Python shell and execute the following commands:


>>> import compileall
>>> compileall.compile_dir("/opt/foo")

When finished, you can exit the shell using the appropriate command:


>>> exit()

Windows

To recursively compile all Python scripts inside the directory C:\Foo, open a Python shell and import the compileall module first.


>>> import compileall

On Windows operating systems a path contains backslashes, which usually are processed as escape characters, so the path has to be given in a certain format. So, you can either escape backslashes by typing two of them


>>> compileall.compile_dir("C:\\Foo")

or give the path in raw format, which does not process any escape characters:


>>> compileall.compile_dir(r"C:\Foo")

When finished, you can exit the shell using the appropriate command:


>>> exit()

Run compiled Python scripts

How to run compiled Python scripts.

Unix-like systems

The compiled files do not have a shebang in the first line anymore. For example, running the compiled file bar.pyc inside the directory /opt/foo like this


$ /opt/foo/bar.pyc

will not work and setting the executable flag

$ chmod +x /opt/foo/bar.pyc

also does not make any difference. You have to run the compiled file explicitly using the Python interpreter:


$ python /opt/foo/bar.pyc

Windows

On Windows systems running compiled Python scripts works as described here, with the difference that the compiled files have the suffix pyc instead of py.