What to do with pyc files when Django or python is used with Mercurial?
Domanda
Just started to use Mercurial. Wow, nice application. I moved my database file out of the code directory, but I was wondering about the .pyc
files. I didn't include them on the initial commit. The documentation about the .hgignore
file includes an example to exclude *.pyc
, so I think I'm on the right track.
I am wondering about what happens when I decide to roll back to an older fileset. Will I need to delete all the .pyc
files then? I saw some questions on Stack Overflow about the issue, including one gentleman that found old .pyc
files were being used. What is the standard way around this?
Soluzione
As mentioned in ms4py's answer, *.pyc are compiled files that will be regenerated on the fly. You wouldn't want to include these when distributing a project.
However, if it happens you have modules that existed before when you roll back changes and *.pyc files are left lying around, strange bugs can appear as pyc files can be execute even if the original python file doesn't exist anymore. This has bitten me a few times in Django when adding and removing apps in a project and switching branches with git.
To clean things up, you can delete every compiled files in your project's directory by running the following shell command in you project's directory:
find . -name '*.pyc' -exec rm {} \;
Altri suggerimenti
Usually you are safe, because *.pyc
are regenerated if the corresponding *.py
changes its content.
It is problematic if you delete a *.py
file and you are still importing from it in another file. In this case you are importing from the *.pyc
file if it is existing. But this will be a bug in your code and is not really related to your mercurial workflow.
Conclusion: Every famous Python library is ignoring their *.pyc
files, just do it ;)
For a more general solution, then ask Mercurial itself about which files it ignores:
$ hg status --ignored
You can make the output safe for xargs
consumption:
$ hg status --ignored --no-status -0 | xargs -0 -delete
(The -0
is important to handle files with spaces in the name.) You can install this as a post-update hook:
[hooks]
post-update = hg status --ignored --no-status -0 | xargs -0 -delete
Finally, Mercurial comes with a purge extension that does something similar:
$ hg purge --all
will delete both untracked and ignored files. If you're okay with losing untracked files, then that command is probably the easiest for you, in particular because it also works on Windows where xargs
may be hard to come by.
Sure if you have a .pyc
file from an older version of the same module python will use that. Many times I have wondered why my program wasn't reflecting the changes I made, and realized it was because I had old pyc files.
If this means that .pyc
are not reflecting your current version then yes you will have to delete all .pyc
files.
If you are on linux you can find . -name *.pyc -delete