296

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

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Avner
  • 5,791
  • 2
  • 29
  • 33

10 Answers10

315

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.

So run your program as python -B prog.py.

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

Nav
  • 19,885
  • 27
  • 92
  • 135
Constantin
  • 27,478
  • 10
  • 60
  • 79
  • 1
    At least on OS X 10.8 running Python 2.7 the environment variable has not effect. – sorin Nov 29 '12 at 13:53
  • 5
    If you're embedding the interpreter (in a C++ program), use "Py_DontWriteBytecodeFlag = 1;" in your source-code. That's a global int declared in pydebug.h. – JimB Jan 04 '14 at 09:56
  • 1
    The environment variable works just fine for me in 2.7, don't know what problems others are having. Thanks! – Thomas Aug 09 '14 at 03:00
  • 8
    Worked for me too on OSX (10.8 & 10.10); @sorin did you export the variable properly? `export PYTHONDONTWRITEBYTECODE=1` – nevelis Nov 19 '14 at 23:52
  • Would using -B/PYTHONDONTWRITEBYTECODE slow down the execution/startup of the python code? – Nagri Dec 13 '18 at 05:57
  • 2
    Do you know how to do the same with pytest? –  Mar 17 '19 at 16:33
  • @Thomas if for some reason the `.pyc` file has an incorrectly more recent modify date than the `.py` file of the same name, the `.pyc` file is not regenerated as it should and the outdated `.pyc` file is executed. – Tarik Apr 30 '22 at 22:21
125
import sys

sys.dont_write_bytecode = True
yprez
  • 14,854
  • 11
  • 55
  • 70
te wilson
  • 1,259
  • 1
  • 8
  • 2
  • 27
    I just tried this and, it does work for imported modules. Specifically, once this variable is set anything imported later won't generate pyc files. This is delightful. Thanks. –  Nov 22 '12 at 19:59
  • Rather adding this in the Parent Module, try adding this in the referenced Script. This works cool. Thanks to @te wilson – Sathy Jun 03 '13 at 08:14
  • 4
    Add this to your `site-packages/usercustomize.py` to make this apply to all your scripts. For me, this directory was `$HOME/.local/lib/python2.6/site-pacakges/usercustomize.py`. Cf. http://docs.python.org/2/tutorial/interpreter.html#the-customization-modules – RobM Jan 20 '14 at 12:13
  • can we add [sys.dont_write_bytecode = True] in settings.py file for all. – Hemant Jul 03 '14 at 06:39
  • 3
    My site-packages was located at: /usr/local/lib/python2.7/site-packages and I had to create usercustomize.py – fIwJlxSzApHEZIl Sep 09 '14 at 22:15
  • 5
    Or in one-line: `import sys; sys.dont_write_bytecode = True` – ET-CS Feb 27 '15 at 06:16
  • Works with Python 2.7.9 on Windows -- just as @tea said above. – mlvljr May 09 '15 at 19:40
  • 1
    `sys.dont_write_bytecode = True` does not work at least in python 2.7 in case of a script that is mitiprocessing versioned. – Learner Oct 11 '16 at 16:04
  • I find this solution easier to use for python scripts as it makes it easier to transfer to other users without having to configure environment variables. – Kurt Rojas Dec 06 '21 at 07:20
23

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.

Jason Baker
  • 192,085
  • 135
  • 376
  • 510
15

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.

Example

If you add the following line to your ./profile in Linux:

export PYTHONPYCACHEPREFIX="$HOME/.cache/cpython/"

Python won't create the annoying __pycache__ directories in your project directory, instead it will put all of them under ~/.cache/cpython/

Rotareti
  • 49,483
  • 23
  • 112
  • 108
11

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"

Brian
  • 116,865
  • 28
  • 107
  • 112
10

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 somefile.pyc.

When you install a utility using setup.py and 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 -B:

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 PYTHONDONTWRITEBYTECODE:

PYTHONDONTWRITEBYTECODE=x myutil
Elwyne
  • 101
  • 1
  • 4
5

Solution for 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 ~/.ipython/profile-default/startup/00-startup.ipy. Instead using the following in your ~.ipython/profile-default/startup/00-startup.py

import sys
sys.dont_write_bytecode=True
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
alpha_989
  • 4,882
  • 2
  • 37
  • 48
4

I have several test cases in a test suite and before I was running the test suite in the Mac Terminal like this:

python LoginSuite.py

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.

Ravil Asadov
  • 65
  • 1
  • 7
2

You could make the directories that your modules exist in read-only for the user that the Python interpreter is running as.

I don't think there's a more elegant option. PEP 304 appears to have been an attempt to introduce a simple option for this, but it appears to have been abandoned.

I imagine there's probably some other problem you're trying to solve, for which disabling .py[co] would appear to be a workaround, but it'll probably be better to attack whatever this original problem is instead.

Logan
  • 1,884
  • 1
  • 12
  • 11
0

As far as I know python will compile all modules you "import". However python will NOT compile a python script run using: "python script.py" (it will however compile any modules that the script imports).

The real questions is why you don't want python to compile the modules? You could probably automate a way of cleaning these up if they are getting in the way.

Alex
  • 49
  • 2
  • I have found frequently that there are stale bytecode `.pyc` files. For some reason, when I change the class/module, the `.pyc` file doesn't get updated. So when I import it after changing the `.py` file, it will still use the `.pyc` file, leading to errors – alpha_989 Feb 10 '18 at 01:27