Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable the creation of the __pycache__ directory #200

Closed
pytestbot opened this issue Oct 9, 2012 · 13 comments
Closed

disable the creation of the __pycache__ directory #200

pytestbot opened this issue Oct 9, 2012 · 13 comments
Labels
type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

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?


@pytestbot
Copy link
Contributor Author

Original comment by Benjamin Peterson (BitBucket: gutworth, GitHub: gutworth):


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

@SharanyaVR
Copy link

Where do we set PYTHONDONTWRITEBYTECODE=1.?

@nicoddemus
Copy link
Member

Anywhere in your environment, .bashrc for example.

@lorie-castillano
Copy link

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!

@nicoddemus
Copy link
Member

Hi @clorieanne,

Not sure, pytest certainly respects that:

write = not sys.dont_write_bytecode
cache_dir = os.path.join(fn_pypath.dirname, "__pycache__")
if write:
try:
os.mkdir(cache_dir)
except OSError:

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?

@golubitsky
Copy link

golubitsky commented Jun 8, 2018

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.

@nicoddemus
Copy link
Member

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

@nicoddemus
Copy link
Member

Opened #3557 as followup.

@xuguozhen
Copy link

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!

@token47
Copy link

token47 commented Aug 18, 2020

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

@fritzlerilan
Copy link

fritzlerilan commented Jun 9, 2021

If you're used python venv built-in functionality for your virtual enviroment:

If in your 'activate' script inside the enviroment folder around line 46 types:
PYTHONDONTWRITEBYTECODE=1
export PYTHONDONTWRITEBYTECODE
the problem is solved with every time you run the application. but... you have to write this anytime you create a new virtual enviroment

@OCEANOFANYTHINGOFFICIAL
Copy link

it will work better

import sys
sys.dont_write_bytecode = True

@yoxenl
Copy link

yoxenl commented Dec 6, 2021

@OCEANOFANYTHINGOFFICIAL this worked, thank you a lot. Quick, easy and simple 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

10 participants