Skip to content

Disable the creation of the __pycache__ directory... From pytest.ini / pyproject.toml #9529

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

Closed
maxsu opened this issue Jan 19, 2022 · 10 comments
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@maxsu
Copy link

maxsu commented Jan 19, 2022

Problem Statement

In order to keep project directory structures clean and non-distracting, I'd like to:

  1. Prevent the creation of __pycache__ directories when running tests
  2. Control this behavior from existing config files (pytest.ini or pyproject.toml)

Use Case

Simple projects that don't benefit from bytecode caching would benefit from a cleaner directory structure. A solution would consolidate pytest configuration and reduce the need for extra files, eg, .gitignore or a test-runner.sh.

Workarounds

  1. Set an environment variable during pytest invokation, eg, PYTHONDONTWRITEBYTECODE=1 pytest (see: disable the creation of the __pycache__ directory #200)
  2. Create a post-test cleanup script to delete the cache
  3. Ignore the cache directories via .gitignore

Caveats:

  • Solutions 1 and 2 require the user to manually configure pytest behavior at invocation or pushes pytest behavior to an extra file (eg. test-runner.sh).
  • Solution 3 cleans up the project at the git repository level but requires a .gitignore file and creates cache directories at a local level.
@The-Compiler
Copy link
Member

FWIW I'm -1 on this. Anything importing your module will lead to Python writing __pycache__, so a knob to only turn this off for pytest only seems to be useful in one specific case (namely when you never import your code, nor use any tools which do so).

@The-Compiler The-Compiler added the type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature label Jan 19, 2022
@RonnyPfannschmidt
Copy link
Member

a possible consideration could be on whether pytest should support putting pyc files into the pytest cache folder based on a argument

but thats not what this is about

im also -1 on a pytest local option for this based on missed errors/issue in the past when running without bytecode

@maxsu
Copy link
Author

maxsu commented Jan 19, 2022

@The-Compiler, unless I'm mistaken test-time creation of __pycache__ directories seems like a distinct behavior from import-time creation. When deploying to a production environment, the runtime should create cache files as necessary. When testing locally, it would be nice to not clutter the directory tree with files irrelevant to the developer.

@RonnyPfannschmidt if testing without writing bytecode can lead to missed errors, that's a serious pain. That's unexpected to me, since the interpreter creates bytecode even if it doesn't cache to disk. On the other hand, if complex test scenarios would force the interpreter to create and re-create the bytecode during testing, this could make a horrifying kind of sense.

A couple questions:

  1. Do we know why failing to write byte code can cause missed tests? Is it replicable, or is it the kind of stochastic horror-bug that strikes with no reason or logic?
  2. Does the risk of missed tests increase if we clean up the cache directory after testing (eg, workaround 2)?

If workaround 2 is safe, then perhaps a solution is a pytest plugin that does post-test cleanup.

@The-Compiler
Copy link
Member

@The-Compiler, unless I'm mistaken test-time creation of __pycache__ directories seems like a distinct behavior from import-time creation. When deploying to a production environment, the runtime should create cache files as necessary. When testing locally, it would be nice to not clutter the directory tree with files irrelevant to the developer.

Do you never import your code (nor use any tools which do so, other than pytest) while developing it? Even just having a script importing from an utils.py or whatever will create a __pycache__. Or even with just a single script file, using the python REPL and import myscript to try something out will.

@The-Compiler The-Compiler added the status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity label Jan 27, 2022
@maxsu
Copy link
Author

maxsu commented Feb 12, 2022

@The-Compiler I think your argument is analagous to this:

Hi, I have a dog named Pytest, but it makes a mess. Can I train it to make less mess?

Look, I have five dogs, and they all make messes. So there's no use training Pytest. Let it be a dog!

But I really do just testing in most sessions, and if I deal with one mess at a time, my environment stays clean.

I would like to implement the feature and get it into the standard build or standard plugins. I would really appreciate suggestions that'll help the PR go through, thanks!

@The-Compiler
Copy link
Member

This rhetoric doesn't seem to be very constructive. I tried to understand your use-case for this, if this is the answer I get, I'm out.

@maxsu
Copy link
Author

maxsu commented Feb 12, 2022

@The-Compiler if I misunderstood and misrepresented your point, then I apologize.

Most of the time I run only pytest, and want the option to run pytest without adding extra files to my source. I'm willing to deal with other tools on their own terms; this is part of an effort to deal with sources of cache files one tool at a time. I think that's reasonable.

@maxsu
Copy link
Author

maxsu commented Mar 20, 2022

@nicoddemus @The-Compiler, I remain convinced that an option to suppress __pycache__ folder creation would nice to have in my own projects, and may be a good first Pytest plugin or PR for me. I'd need a pointer as to where to make this change, and I might reach out with a mentorship request in the future. For the help, I would be willing to handle other more important issues as well. I'll close this issue now, so as to keep the backlog clear of low value issues. Regards.

@maxsu maxsu closed this as completed Mar 20, 2022
@nicoddemus
Copy link
Member

I think @The-Compiler's point is that any import you do in your test modules will result in __pycache__ being created, as this is a Python feature (not pytest exclusive). Setting PYTHONDONTWRITEBYTECODE=1 (as you mention) solves the problem, both for pytest generated files (due to assert rewriting) as well as normal Python imports, so I think the suggested solution is to use that environment variable: it is officially supported by the Python runtime and honored by pytest.

@Cheaterman
Copy link

I had a bit of an interesting SNAFU today where pyc's would be caching a host file path that would then be incorrect when running tests inside docker (python-dotenv blew up, I'm guessing because it does... interesting... tricks to find its own location - possibly something I should report there?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

5 participants