How to avoid .pyc files?

From “What’s New in Python 2.6 – Interpreter Changes”:

Python can now be prevented from
writing .pyc or .pyo files by
supplying the -B switch to the Python
interpreter, or by setting the
PYTHONDONTWRITEBYTECODE environment
variable before running the
interpreter. This setting is available
to Python programs as the
sys.dont_write_bytecode variable, and
Python code can change the value to
modify the interpreter’s behaviour.

So run your program as python -B prog.py.

Update 2010-11-27: Python 3.2 addresses the issue of cluttering source folders with .pyc files by introducing a special __pycache__ subfolder, see What’s New in Python 3.2 – PYC Repository Directories.

NOTE: The default behavior is to generate the bytecode and is done for “performance” reasons (for more information see here for python2 and see here for python3).

Leave a Comment