[Python-ideas] add a hash to .pyc to don't mess between .py and .pyc

Steven D'Aprano steve at pearwood.info
Sun Aug 14 20:49:32 EDT 2016


On Mon, Aug 15, 2016 at 01:05:47AM +0200, Xavier Combelle wrote:
> I have stumbled upon several time with the following problem.
> I delete a module and the .pyc stay around. and by "magic", python still
> use the .pyc

Upgrade to Python 3.2 or better, and the problem will go away.

In 3.2 and above, the .pyc files are stored in a separate __pycache__ 
directory, and are only used if the .py file still exists. In Python 3.1 
and older, you have:

# directory in sys.path
spam.py
spam.pyc
eggs.py
eggs.pyc

and if you delete eggs.py, Python will still use eggs.pyc. But in 3.2 
and higher the cache keeps implementation and version specific byte-code 
files:

spam.py
eggs.py
__pycache__/
+-- spam-cpython-32.pyc
+-- spam-cpython-35.pyc
+-- spam-pypy-33.pyc
+-- eggs-cpython-34.pyc
+-- eggs-cpython-35.pyc


If you delete the eggs.py file, the eggs byte-code files won't be used. 
Byte-code only modules are still supported, but you have to explicitly 
opt-in to that by moving the .pyc file out of the __pycache__ directory 
and renaming it.

See PEP 3147 for more details:

https://www.python.org/dev/peps/pep-3147/


-- 
Steve


More information about the Python-ideas mailing list