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.

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).

  • The generation of bytecode .pyc files is a form of caching (i.e. greatly improves average performance).
  • Configuring python with PYTHONDONTWRITEBYTECODE=1 can be bad for python performance (for python2 see https://www.python.org/dev/peps/pep-0304/ and for python3 see https://www.python.org/dev/peps/pep-3147/ ).
  • If you are interested in the performance impact please see here https://github.com/python/cpython .

Leave a Comment