Python File Extensions (py, pyc, pyw, pyo, pyd) and Deployment Options
File extensions
You probably know about .py
and .pyc
, but Python uses a few other extensions as well.
.py
Plain source files. On Windows, double-clicking runs them with python.exe
.
.pyw
Another source extension. On Windows, double-clicking launches pythonw.exe
, which skips the console window. Handy for GUI apps that do not need a terminal.
.pyc
Whenever Python executes a module it caches a compiled bytecode file alongside it. These .pyc
files can be executed directly but are not human-readable.
.pyo
Optimized bytecode generated with the -O
flag; similar to .pyc
.
.pyd
Shared libraries written in another language (often C or C++) and importable from Python. They are not generated from .py
files.
Deployment choices
I mostly ship Windows programs; other platforms usually just get the source.
Package as a standalone executable
For public releases the usual approach is to bundle everything into an .exe
. Users do not need Python installed, and the format feels familiar. The downside is the size and occasional compatibility hiccups.
I typically use PyInstaller:
pyinstaller -F example.py
-F
produces a single executable. For GUI apps that do not need a console window add -w
:
pyinstaller -w -F example.py
Distribute compiled .pyc
/ .pyo
If users have Python installed and you would rather not share source code, ship the bytecode.
Python does not generate .pyc
for scripts run directly—you must compile them:
1 | import py_compile |
Compile entire directories:
1 | import compileall |
Create a .pyo
:
python -O -m py_compile file.py
For GUI apps without a console, create a small .pyw
launcher that simply imports the compiled module.
Distribute source
Open-source projects usually share the .py
files. As long as the user has a matching environment, the code runs.
References: