Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid .pyc files?

Tags:

python

Can I run the python interpreter without generating the compiled .pyc files?

like image 809
Avner Avatar asked Sep 30 '08 18:09

Avner


2 Answers

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 .
like image 121
Constantin Avatar answered Oct 06 '22 01:10

Constantin


import sys  sys.dont_write_bytecode = True 
like image 23
te wilson Avatar answered Oct 06 '22 02:10

te wilson