52

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?

3
  • 1
    Possible duplicate of ignoring any 'bin' directory on a git project
    – melpomene
    May 25, 2019 at 22:14
  • 1
    Possible duplicate of Best practices for adding .gitignore file for Python projects?
    – Rumid
    May 25, 2019 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, 2019 at 2:25

5 Answers 5

46

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

1
  • 1
    This works with tree --gitignore whereas just __pycache__/ only ignores the top level one.
    – Paul Evans
    Sep 4, 2022 at 16:18
35

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

8
  • 6
    You don't need **. You can just say __pycache__/.
    – melpomene
    May 25, 2019 at 22:17
  • 4
    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, 2019 at 22:24
  • @marlon Displays them how exactly?
    – melpomene
    May 25, 2019 at 22:27
  • 1
    @marlon The asterisk is not needed. I guess you should take a look at this: stackoverflow.com/questions/1274057/…
    – alfunx
    May 25, 2019 at 22:35
  • 2
    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, 2022 at 5:48
2

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

2

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

git rm --cached */__pycache__/*
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.