You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a .gitignore file to each __pycache__ folder #141081
When creating a __pycache__ folder, add a .gitignore file with the content
# Created by CPython*
I've seen this pattern appear in a few places (coverage.py's htmlcov folder, .venv, .pytest_cache) and it saves projects from having to add such things to their main .gitignore.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
My biggest question is how do we document this (I'm sure someone somewhere is checking in their __pycache__ files on purpose)? I'm assuming we will tweak this as we just deem reasonable based on whatever VCS is most popular at the time, but we should be clear in the docs that what's supported can change and there won't be any warning beyond What's New.
This sounds like unnecessary noise for things like container images. There are tons of repository templates including in GitHub which will initialize your repo with top-level .gitignore for Python.
I think we are overthinking it. The content of __pycache__ is an implementation detail. It was already changed in the past (from .pyc and .pyo to .cpython-315.opt-2.pyc) and depends on the Python version, so if you use the same source tree with different Python versions it can be full of abandoned stuff). Simply, either add .gitignore, or don't.
I don't think adding it will help much, but it will not harm. If you worry about disk space, you should seriously consider precompiling bytecode and packing it in a zip file (even without compression it will save a lot of space for small files).
Reacted by Zachary Ware, Stan Ulbrych, Brett Cannon, Bartosz Sławecki, Gregory P. Smith and Hugo van Kemenade
and have release manager @hugovk promote it in release notes asking people to pipe up if it causes problems and make the final call based on any feedback before the RC phase.
test_with_pip (test.test_venv.EnsurePipTest.test_with_pip) ... FAIL
======================================================================
FAIL: test_with_pip (test.test_venv.EnsurePipTest.test_with_pip)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_venv.py", line 1047, in test_with_pipself.do_test_with_pip(False)
~~~~~~~~~~~~~~~~~~~~~^^^^^^^
File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_venv.py", line 1023, in do_test_with_pipself.assert_pip_not_installed()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_venv.py", line 906, in assert_pip_not_installedself.assertEqual(out.strip(), "OK")
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^AssertionError: '' != 'OK'
+ OK
The problem was that when pip uninstalls itself, it removes its own files, and then tries to remove empty directories like __pycache__. But __pycache__ contains our .gitignore so the dir removal fails.
#142745 is a possible fix to not add .gitignore in site-packages dirs, and let the package managers handle those.
However, it hardcodes site-packages. We should maybe use sysconfig.get_path('purelib') instead in case the user has reconfigured to use something else, such as dist-packages in Ubuntu, but we can't import sysconfig in _bootstrap_external.py because it's too early and can result in a circular import.
Activity
serhiy-storchaka commentedon Nov 6, 2025
Is not
__pycache__in a single top-level.gitignorefile enough?hugovk commentedon Nov 6, 2025
Similarly, we added a
.gitignoreto venv dirs in 3.13: #83417.gvanrossum commentedon Nov 6, 2025
But that would have to be added by every user for every repo.
I would like the repo .gitignore to be about artifacts of the software in the repo, not artifacts of tools if we can avoid it.
zware commentedon Nov 6, 2025
Creating it ourselves also sends a clear signal that you really shouldn't be checking this stuff in.
brettcannon commentedon Nov 6, 2025
My biggest question is how do we document this (I'm sure someone somewhere is checking in their
__pycache__files on purpose)? I'm assuming we will tweak this as we just deem reasonable based on whatever VCS is most popular at the time, but we should be clear in the docs that what's supported can change and there won't be any warning beyond What's New.gvanrossum commentedon Nov 6, 2025
Did we update any docs for the
venvmodule when we added the same thing there?Is
__pycache__documented at all? Wherever that is, is where the docs should be updated for this.hugovk commentedon Nov 7, 2025
We added a
--without-scm-ignore-filesflag and ascm_ignore_filesparameter and documented those:Although the What's New is the best overview:
It's mentioned on a dozen pages but doesn't seem to have its own "home":
Perhaps this would be a good place to put it:
There's a PR up at #141162.
nanonyme commentedon Nov 7, 2025
This sounds like unnecessary noise for things like container images. There are tons of repository templates including in GitHub which will initialize your repo with top-level .gitignore for Python.
nanonyme commentedon Nov 7, 2025
If this is done, please make it opt-outable at least. I am quite surprised also to hear that venv went ahead with this feature.
hugovk commentedon Nov 7, 2025
It's opt-out for venv using
--without-scm-ignore-files:https://docs.python.org/3/library/venv.html#cmdoption-venv-without-scm-ignore-files
nanonyme commentedon Nov 7, 2025
I was referring to if adding this became a Python default, not just venv. There should really be an opt-out.
brettcannon commentedon Nov 7, 2025
This is a bit off-topic for this issue. If you want to propose reverting the default behaviour for venv then please open another issue.
17 remaining items
serhiy-storchaka commentedon Dec 8, 2025
I think we are overthinking it. The content of
__pycache__is an implementation detail. It was already changed in the past (from.pycand.pyoto.cpython-315.opt-2.pyc) and depends on the Python version, so if you use the same source tree with different Python versions it can be full of abandoned stuff). Simply, either add.gitignore, or don't.I don't think adding it will help much, but it will not harm. If you worry about disk space, you should seriously consider precompiling bytecode and packing it in a zip file (even without compression it will save a lot of space for small files).
brettcannon commentedon Dec 12, 2025
I think I agree.
Any core devs against merging w/o an opt-out and seeing how it goes during the alpha and beta phases?
gpshead commentedon Dec 13, 2025
yeah lets just see how it goes.
gpshead commentedon Dec 13, 2025
and have release manager @hugovk promote it in release notes asking people to pipe up if it causes problems and make the final call based on any feedback before the RC phase.
gh-141081: Add a `.gitignore` file to `__pycache__` folders (#141162)
hugovk commentedon Dec 15, 2025
This will be in tomorrow's 3.15.0a3, is called out in What's New's release highlights and I'll also put this in the release notes:
Thanks all!
gvanrossum commentedon Dec 15, 2025
Thanks all indeed!
hugovk commentedon Dec 15, 2025
Re-opening.
Buildbots failed after merging the PR: #141162 (comment)
The problem was that when pip uninstalls itself, it removes its own files, and then tries to remove empty directories like
__pycache__. But__pycache__contains our.gitignoreso the dir removal fails.#142745 is a possible fix to not add
.gitignoreinsite-packagesdirs, and let the package managers handle those.However, it hardcodes
site-packages. We should maybe usesysconfig.get_path('purelib')instead in case the user has reconfigured to use something else, such asdist-packagesin Ubuntu, but we can'timport sysconfigin_bootstrap_external.pybecause it's too early and can result in a circular import.But as @encukou asks:
Thoughts?
RM hat: we may need to revert this for tomorrow's a3 so we have more time for this.
Revert "pythongh-141081: Add a `.gitignore` file to `__pycache__` fol…
brettcannon commentedon Dec 15, 2025
At what directory level? Virtual environment (which Alyssa has an idea)? All installed projects? Each installed project?
gh-141081: Revert "Add a `.gitignore` file to `__pycache__` folders (#…
pythongh-141081: Add a `.gitignore` file to `__pycache__` folders (py…
pythongh-141081: Revert "Add a `.gitignore` file to `__pycache__` fol…