5

It seems that sometimes when I pull from the git the old .pyc runs instead of the new pulled .py file is there a way to automatically clear the .pyc file so it runs always the fresh version ?

Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179

2 Answers2

13

The old .pyc is automatically cleared by Python, provided the modified date on the .py file is newer.

You could manually delete all .pyc files in a directory structure with:

find . -name \*.pyc -delete

and Python will re-create them as modules are imported. You can also run:

python -m compileall .

to force a compilation.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I don't commit them, I don't know exactly how git does time the files – Eduard Florinescu Sep 24 '12 at 15:55
  • 4
    Ah, I see; you pull *older* `.py` files from git thus the newer `.pyc` files are used instead of the changed source. – Martijn Pieters Sep 24 '12 at 15:57
  • It seems it times them the time of the pull, I don't know what I done wrongly when it happened – Eduard Florinescu Sep 24 '12 at 15:59
  • 2
    Yes, git sets the modified time to the time of *commit*; your `.pyc` files on the other hand match whatever the modified time was of the `.py` file was at time of compilition. If you modify a `.py` file, run python, then roll back to an older version of the `.py` file, the `.pyc` file is not necessarily updated. – Martijn Pieters Sep 24 '12 at 16:01
  • Its hard to investigate what happened post factum, better to apply solution, +1 and accept for the answer, – Eduard Florinescu Sep 24 '12 at 16:04
1

Another option to prevent the creation of .pyc file is to pass options -B to python

python -B file.py

-B option from man:

-B     Don't write .py[co] files on import. See also PYTHONDONTWRITEBYTECODE.

also you can specify it at the beginning of the .py file with sha-bang:

#!/bin/python -B 
Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179