What to do with pyc files when Django or python is used with Mercurial?

Just started using Mercurial. Wow, nice application. I moved my database file from the code directory, but I was interested to know about .pyc files. I did not include them in the initial commit. The documentation for the .hgignore file contains an example *.pyc exception, so I think I'm on the right track.

I am wondering what will happen when I decide to return to the old set of files. Do I need to delete all .pyc files? I saw some stack overflow questions about this issue, including one gentleman who found old .pyc files. What is the standard way of doing this?

+7
source share
4 answers

As mentioned in ms4py's answer, * .pyc are compiled files that will be updated on the fly. You will not want to include them when distributing the project.

However, if this happens, you have the modules that existed before, when you roll back the changes, and the * .pyc files remain lying, strange errors may appear, since the pyc files can be executed even if the original python file no longer exists. once bit me in Django when adding and removing applications in a project and switching branches using git.

To clear everything, you can delete all compiled files in the project directory by running the following shell command in the project directory:

 find . -name '*.pyc' -exec rm {} \; 
+13
source

Usually you are safe because *.pyc restored if the corresponding *.py changes its contents.

This is a problem if you delete the *.py file and you are still importing it from another file. In this case, you import from the *.pyc file, if one exists. But this will be a mistake in your code and is not really related to your mercurial workflow.

Conclusion Every famous Python library ignores its *.pyc files, just do it;)

+5
source

For a more general solution, ask Mercurial which files it ignores:

 $ hg status --ignored 

You can conclude that it is safe for xargs consumption:

 $ hg status --ignored --no-status -0 | xargs -0 -delete 

( -0 it is important to handle files with spaces in the name.) You can set this as a hook after updating:

 [hooks] post-update = hg status --ignored --no-status -0 | xargs -0 -delete 

Finally, Mercurial comes with a purge extension that does something like this:

 $ hg purge --all 

will delete both unprocessed and ignored files. If everything is fine with the loss of irreproducible files, this command is probably the easiest for you, in particular because it also works on Windows, where xargs can be hard to find.

+3
source

Of course, if you have a .pyc file from an older version of the same module, then python will use this. Many times I wondered why my program does not reflect the changes I made, and realized that this was due to the fact that I had old pyc files.

If this means that .pyc does not reflect your current version, then yes, you will have to delete all .pyc files.

If you are using Linux, you can find . -name *.pyc -delete find . -name *.pyc -delete

0
source

All Articles