Can I run the python interpreter without generating the compiled .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
variable before running the
interpreter. This setting is available
to Python programs as the
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.
import sys sys.dont_write_bytecode = True
There actually IS a way to do it in Python 2.3+, but it’s a bit esoteric. I don’t know if you realize this, but you can do the following:
$ unzip -l /tmp/example.zip Archive: /tmp/example.zip Length Date Time Name -------- ---- ---- ---- 8467 11-26-02 22:30 jwzthreading.py -------- ------- 8467 1 file $ ./python Python 2.3 (#1, Aug 1 2003, 19:54:32) import sys sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path import jwzthreading jwzthreading.__file__ '/tmp/example.zip/jwzthreading.py'
According to the zipimport library:
Any files may be present in the ZIP archive, but only files .py and .py[co] are available for import. ZIP import of dynamic modules (.pyd, .so) is disallowed. Note that if an archive only contains .py files, Python will not attempt to modify the archive by adding the corresponding .pyc or .pyo file, meaning that if a ZIP archive doesn’t contain .pyc files, importing may be rather slow.
Thus, all you have to do is zip the files up, add the zipfile to your sys.path and then import them.
If you’re building this for UNIX, you might also consider packaging your script using this recipe: unix zip executable, but note that you might have to tweak this if you plan on using stdin or reading anything from sys.args (it CAN be done without too much trouble).
In my experience performance doesn’t suffer too much because of this, but you should think twice before importing any very large modules this way.
In 2.5, theres no way to suppress it, other than measures like not giving users write access to the directory.
In python 2.6 and 3.0 however, there may be a setting in the sys module called “dont_write_bytecode” that can be set to suppress this. This can also be set by passing the “-B” option, or setting the environment variable “PYTHONDONTWRITEBYTECODE”
You can set
sys.dont_write_bytecode = True in your source, but that would have to be in the first python file loaded. If you execute
python somefile.py then you will not get
When you install a utility using
entry_points= you will have set
sys.dont_write_bytecode in the startup script. So you cannot rely on the “default” startup script generated by setuptools.
If you start Python with python file as argument yourself you can specify
python -B somefile.py
somefile.pyc would not be generated anyway, but no
.pyc files for other files imported too.
If you have some utility
myutil and you cannot change that, it will not pass -B to the python interpreter. Just start it by setting the environment variable
Starting with Python 3.8 you can use the environment variable
PYTHONPYCACHEPREFIX to define a cache directory for Python.
From the Python docs:
If this is set, Python will write .pyc files in a mirror directory tree at this path, instead of in pycache directories within the source tree. This is equivalent to specifying the -X pycache_prefix=PATH option.
If you add the following line to your
./profile in Linux:
Python won’t create the annoying
__pycache__ directories in your project directory, instead it will put all of them under
I have several test cases in a test suite and before I was running the test suite in the Mac Terminal like this:
Running the command this way my directory was being populated with .pyc files. I tried the below stated method and it solved the issue:
python -B LoginSuite.py
This method works if you are importing test cases into the test suite and running the suite on the command line.
ipython 6.2.1 using python 3.5.2 (Tested on Ubuntu 16.04 and Windows 10):
Ipython doesn’t respect
%env PYTHONDONTWRITEBYTECODE =1 if set in the
ipython interpretor or during startup in
Instead using the following in your
import sys sys.dont_write_bytecode=True