67

The __pycache__ directory is annoying when working with git updates. Whenever I use git status, a lot of .pyc files show up. How can I conveniently list the __pycache__ folder in my .gitignore file so that they won't show up when using git status?

For example:

core/__pycache__/utils.cpython-36.pyc
core/__pycache__/version.cpython-36.pyc
core/actions/__pycache__/__init__.cpython-36.pyc
core/actions/__pycache__/action.cpython-36.pyc

Do I have to list all the individual __pycache__files into the gitignore file?

melpomene
  • 84,125
  • 8
  • 85
  • 148
marlon
  • 6,029
  • 8
  • 42
  • 76
  • 1
    Possible duplicate of [ignoring any 'bin' directory on a git project](https://stackoverflow.com/questions/1470572/ignoring-any-bin-directory-on-a-git-project) – melpomene May 25 '19 at 22:14
  • 1
    Possible duplicate of [Best practices for adding .gitignore file for Python projects?](https://stackoverflow.com/questions/3719243/best-practices-for-adding-gitignore-file-for-python-projects) – Rumid May 25 '19 at 22:15
  • It sounds like you have *already committed* the `*,pyc` files in some existing commits. These existing commits can never be changed—those pycache files are in them forever. You can make new commits that are like the old commits, but don't have the `*.pyc` files in them, and switch to using the new commits and never use the old ones ever again, which is viable as long as other people on the project also switch over. – torek May 26 '19 at 02:25

5 Answers5

68

Use **/__pycache__/ this to ignore all pycache folder across the repo

hymenoby
  • 781
  • 5
  • 4
37

__pycache__/. The ending slash indicates it is a directory and will ignore any files beneath it.

marsheth
  • 822
  • 6
  • 9
  • 7
    You don't need `**`. You can just say `__pycache__/`. – melpomene May 25 '19 at 22:17
  • 5
    I added "__pycache__" to my .gitignore and then run "git status", it still displays all the .pyc file. It doesn't effect. – marlon May 25 '19 at 22:24
  • @marlon Displays them how exactly? – melpomene May 25 '19 at 22:27
  • In untracked files? What about `__pycache__/*` ? – marsheth May 25 '19 at 22:30
  • 2
    @marlon The asterisk is not needed. I guess you should take a look at this: https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore – alfunx May 25 '19 at 22:35
  • 3
    If you've ALREADY added the `__pycache__` directory files (via `git add ...`), then running `git status` will still show the `__pycache__` directory files - even after your `.gitignore` updates. You'll need to reset (`git reset`) and then run `git status` to see if the .gitignore updates worked. Afterward, you can then add the files you want to commit. – cjn Feb 14 '22 at 05:48
  • @cjn You should absolutely write that as answer, everyone searching this question up will think that adding this line will fix their git. Help future readers out! – Shayan Feb 20 '22 at 13:01
  • @cjn I commited __pycache__ folder lots of commits before, i'm ignoring __pycache_folder but doesn't work cause i made several commits before with that folder added. – Nachengue Apr 21 '22 at 21:53
4

I used this command in my root directory and it worked for me

git rm --cached */__pycache__/*
3

I only wrote:

__pycache__

Worked for me and now ignoring all file in __pycache__ in any directory.

Don't forget to delete all later cached files with __pycache__ .

Stas Oknedis
  • 156
  • 2
  • 5
1

For windows users, please check that your .gitignore file uses \n or \r\n as EOL (end of line), I had \r in my .gitignore file and the file rules were completely ignored in the windows version (git version 2.34.1.windows.1), in which case __pycache__/ was also ignored. After changing the EOL to \n, all the __pycache__ folders were ignored as expected.

theroot
  • 21
  • 1
  • 2