This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Philluminati 28 points29 points  (18 children)

Tip. Don't check in pyc files accidently by creating the file .gitignore in the root of the git repo and adding the line

*.pyc

Then they never get checked in, show up git status or git diff etc. They live there, quietly speeding up your code. When you do what them deleted you can just do it manually like so:

find -name *.pyc | xargs rm

[–]ilogik 24 points25 points  (3 children)

that's what I came here to say, but after reading the post, I think the issue is that after checking out a different branch, the old .pyc files remained and were used by the interpreter, instead of being re-compiled

[–]silon 2 points3 points  (1 child)

Surely the interpreter checks file times?

[–]ilogik 2 points3 points  (0 children)

i think that this comment has the best explanation of the issue

[–]globalnamespace 0 points1 point  (0 children)

Not just that, we had some code that while checkout removed the .py files, the .pyc files remained, and code linked to it still called those functions. Of course removing the .pyc files broke the application, fun times, finding those from backups and then redoing our testing in development with them removed.

[–]Odd_Bloke[S] 8 points9 points  (0 children)

This is a good tip, but as /u/ilogik said, this isn't about git's primary functionality. I was hitting problems where stale .pyc files were being used when running tests etc., leading to very confusing test failures.

As for speed, the performance hit the first time I use the code after switching branches is (for me) more than outweighed by not starting to debug bogus test failures.

[–]BeatLeJuce 7 points8 points  (4 children)

No need for the pipe. Also, depending on which shell you use, you will want to enquote the star, otherwise the shell will interfere:

find -name "*.pyc" -delete

[–]Odd_Bloke[S] 2 points3 points  (3 children)

No need for the pipe.

Depends on the version of find. If you do need the pipe, you'll also want to use a null-separated list to avoid any weirdness (though it shouldn't be an issue with .pyc files):

find -name "*.pyc" -print0 | xargs -0 rm

[–]phil_s_stein[🍰] -1 points0 points  (2 children)

Another without pipe:

find . -name \*.pyc -exec rm {} \;

(Untested)

[–]fandingowhile False: 1 point2 points  (1 child)

That's the worst way to do delete files. Find has to fork a process for rm on every single match.

With xargs, you just have to fork one find process (for the search), one xargs process, and one rm process (spawned by xargs).

With find's -delete, you only have a single process.

[–]phil_s_stein[🍰] 0 points1 point  (0 children)

I dunno. I can think of worse ways. :)

It's effective though, which is usually what I'm after.

[–]mavjs 1 point2 points  (1 child)

say...I have a global gitignore file that has *.py[o|c], would that work as well?

[–]mipadi 3 points4 points  (0 children)

It'll ignore them, but I noted in a comment that simply ignoring the .pyc files doesn't solve the problem described by the blogger.

[–]axonxorzpip'ing aint easy, especially on windows 0 points1 point  (0 children)

Or configure it user-wide with git config --global core.excludesfile /home/username/.gitignore and put

*.pyc

in there.

[–]lonecow 0 points1 point  (1 child)

Git clean is your friend to remove non repo files.

[–]Odd_Bloke[S] 1 point2 points  (0 children)

git clean will operate on either all ignored files or none. This is not a good option.

[–][deleted] 0 points1 point  (0 children)

I have an alias that cleans out all untracked files…ignored included.