Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop python from generating pyc files

Tags:

python

pyc

When I change my Python code, I often need to delete the associated pyc file, or Python will not regenerate it and will run the old code. Is there a way to tell Python not to generated pyc files?

like image 749
maolingzhi Avatar asked Feb 23 '12 06:02

maolingzhi


People also ask

How do I prevent PYC files?

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.

How do I stop Python from making Pycache?

Suppressing the creation of __pycache__ Alternatively, you can set PYTHONDONTWRITEBYTECODE environment variable to any non-empty string. Again, this will prevent Python from trying to write . pyc files. Note that both approaches are equivalent.

Why does Python make PYC files?

pyc files are created by the Python interpreter when a . py file is imported. They contain the "compiled bytecode" of the imported module/program so that the "translation" from source code to bytecode (which only needs to be done once) can be skipped on subsequent imports if the . pyc is newer than the corresponding .

Do I need to keep PYC files?

There's no harm in deleting them (. pyc), but they will save compilation time if you're doing lots of processing. Python is an interpreted language , as opposed to a compiled one, though the distinction can be blurry because of the presence of the bytecode compiler.


1 Answers

Edit the .bashrc file and add the following lines to the very end of the file:

PYTHONDONTWRITEBYTECODE=True
export PYTHONDONTWRITEBYTECODE

Restart your terminal or do the following:

$ source ~/.bashrc
like image 125
Gopinath Langote Avatar answered Oct 02 '22 09:10

Gopinath Langote