Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 581
kd4ttc Avatar asked Jun 06 '12 18:06

kd4ttc


People also ask

Do you need Python installed to run PYC?

They do not need to install any particular version of Python or any modules. They do not need to have Python installed at all. The output of PyInstaller is specific to the active operating system and the active version of Python.

How do I run a .PYC file in Python?

. pyc files are created by the Python interpreter when a . py file is imported. They contain the "compiled bytecode" of the imported module/program so that the "translation" from source code to bytecode (which only needs to be done once) can be skipped on subsequent imports if the .

Does PYC run faster than py?

A program doesn't run any faster when it is read from a ". pyc" or ". pyo" file than when it is read from a ". py" file; the only thing that's faster about ".

Should I delete Pycache?

__pycache__ is a folder containing Python 3 bytecode compiled and ready to be executed. I don't recommend routinely laboriously deleting these files or suppressing creation during development as it wastes your time.


2 Answers

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 {} \;
like image 161
JeromeParadis Avatar answered Sep 28 '22 05:09

JeromeParadis


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 ;)

like image 43
schlamar Avatar answered Sep 28 '22 06:09

schlamar