Making Compiled Python Files
Like C and C++, the free, open-source Python programming language provides a way to compile source code. Usually, compiled files load faster and execute more quickly and efficiently. Unlike C and C++, however, Python compiles to interpreted virtual machine byte-code instead of raw machine code. Python automatically compiles imported modules and saves the byte-code in files with ".pyc" file extensions. When the Python interpreter is invoked with the "-O" parameter, it will create an optimized compiled file with the ".pyo" extension. The next time that module is imported, the interpreter will load the compiled file.
Things You'll Need
- Python interpreter version 2.6 or above installed
- Plain-text text editor such as Notepad
Instructions
-
Compile PYC Files
-
1
Click on the "Start" button. Click clicking on "All Programs." Click the "Accessories" menu option. Click "Notepad" to launch the text editor.
-
2
Type the following code into the text editor. Alternatively, select the code with the mouse, press “Control-C” to copy it, then press “Control-V” to paste the code into the editor. Replace "..." in front of the "print" statement with three spaces.
def test():
...print "This is a Python file"
-
-
3
Click the "File" menu item. Select the "Save" option. Save under the file name "test.py."
-
4
Click on the "Start" button. Click the "Run" menu option. Type the word "command" into the input box labeled "Open:" then click "OK."
-
5
Type “C:\Python\python” then press the enter key to load the Python interpreter. Some Windows Python interpreter installers use a version-specific directory. For instance, if the version of Python on your system is 2.7, you start it by typing “C:\Python27\python” at the command prompt then pressing the “Enter” key.
-
6
Type "import test" at the Python prompt then press the "Enter" key. Press "Control-z" to exit Python.
Type "dir test.*" at the Windows command prompt to verify that the file "test.pyc" was created.
Compiling PYO Files.
-
7
Type "del test.pyo" at the Windows command prompt. Type "dir test.*" at the Windows command prompt to verify that the file "test.pyo" was deleted.
-
8
Type “C:\Python\python -O” then press the enter key to load the Python interpreter. Some Windows Python interpreter installers use a version-specific directory. For instance, if the version of Python on your system is 2.7, you start it by typing “C:\Python27\python -O” at the command prompt then pressing the “Enter” key.
-
9
Type "import test" at the Python prompt then press the "Enter" key. Type "test.test()" at the Python command prompt then press the "Enter" key. The interpreter should display, "This is a Python file."
-
10
Press "Control-Z" to exit the Python interpreter.
-
11
Type "dir test.*" at the Windows command prompt to verify that the file "test.pyo" was created.
-
1
Tips & Warnings
Use the "compileall" module to compile all the Python files within a specific directory to ".pyc" or ".pyo" files. Load your Python interpreter then type "import compileall" at the Python command prompt. If your Python ".py" files are located in the directory "C:\pythonfiles", type "compileall.compile_dir('C:\pythonfiles\')" at the Python command prompt. If you want ".pyo" files, first invoke the interpreter by typing "C:\Python\python -O” at the Windows command prompt.
Files compiled with one version of Python may not work with other versions. Newer versions of the language may have a different, incompatible method of producing Python byte-code. Specify the version of Python needed to run your compiled scripts when you share them with others. Distributing the source code, however, is the best way to make your code available to more users.
References
Resources
- Photo Credit Stockbyte/Retrofile/Getty Images