Pytest: disable the creation of the __pycache__ directory

Created on 9 Oct 2012  路  10Comments  路  Source: pytest-dev/pytest

Originally reported by: BitBucket: astrofrog, GitHub: astrofrog


Is there a way to disable the creation of the pycache directories, or cleanup these directories after testing, without installing any plugin or other packages?


bug

Most helpful comment

In order to prevent both ./.pytest_cache _and_ ./**/__pycache__ from being generated.

The only thing that worked for me was using both the above solution _as well as_ disabling caching

PYTHONDONTWRITEBYTECODE=1 python -m pytest -p no:cacheprovider

(source)

As far as I can tell, the above command does not generate any artifacts. IMHO no:cacheprovider should be the default. If users want performance enhancements they should have to explicitly specify that.

All 10 comments

_Original comment by_ Benjamin Peterson (BitBucket: gutworth, GitHub: gutworth):


You can run python with -B or set PYTHONDONTWRITEBYTECODE=1.

Where do we set PYTHONDONTWRITEBYTECODE=1.?

Anywhere in your environment, .bashrc for example.

Hello everyone, I have the same problem with creation of __pycache__ I added PYTHONDONTWRITEBYTECODE=1 in my /etc/environment but it still creates. Is there other way? thanks!

Hi @clorieanne,

Not sure, pytest certainly respects that:

https://github.com/pytest-dev/pytest/blob/18157659ca05c6088e3c012902d819df773823c5/src/_pytest/assertion/rewrite.py#L113-L118

Also I just tested this and setting the PYTHONDONTWRITEBYTECODE=1 correctly prevents pytest from writing the __pycache__ folder.

Perhaps is some other library creating the folder?

In order to prevent both ./.pytest_cache _and_ ./**/__pycache__ from being generated.

The only thing that worked for me was using both the above solution _as well as_ disabling caching

PYTHONDONTWRITEBYTECODE=1 python -m pytest -p no:cacheprovider

(source)

As far as I can tell, the above command does not generate any artifacts. IMHO no:cacheprovider should be the default. If users want performance enhancements they should have to explicitly specify that.

@golubitsky indeed .pytest_cache is created regardless... we should think how better support running pytest on readonly file systems.

FWIW cacheprovider is not about performance, but to persist data between test sessions, and provides --lf and --ff flags. And sorry, but performance enhancements should be enabled by default if that were the case, as many other things in Python are. For example you get accelerator modules when importing some builtin modules (like pickle) automatically by default.

Opened #3557 as followup.

Hello, everyone
I have the same problem when I build Python package by setup.py, and how do I package and install the Python package without creating __pycache__ directory.
Thanks!

@SharanyaVR You need to export the PYTHONDONTWRITEBYTECODE variable, not enough to only create it in the current shell.

Was this page helpful?
0 / 5 - 0 ratings