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?
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.
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.
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 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With