This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python does not warn in module .py files does not exist if there is still a .pyc file
Type: Stage: resolved
Components: Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: Julian, brett.cannon, ezio.melotti, lennartyseboodt, ncoghlan, pitrou
Priority: normal Keywords:

Created on 2011-12-03 12:18 by lennartyseboodt, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pythonbug.tar.gz lennartyseboodt, 2011-12-03 12:18
Messages (5)
msg148791 - (view) Author: (lennartyseboodt) Date: 2011-12-03 12:18
Python does not warn/error when importing a module where the module.py file no longer exists, but there is still a .pyc file present.

It also does not warn when the imported file.py is a symlink pointing nowhere. As long as there is still a .pyc file with the correct name it does not warn/error.

Attached is a minimal set to demonstrate. Execute test.py.
msg148794 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-12-03 12:27
Python doesn't require the .py to run the code if there's a .pyc.  This is expected so there's no need to give a warning.

If the .py is present but it's a symlink pointing nowhere, it might make sense to give a warning before falling back silently on the .pyc.
msg148820 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-12-03 19:49
Indeed, this is a feature, not a bug. Lone pyc files are used by some people to provide "binary only" distributions of Python software (I'm not really saying it's a good idea, but some people rely on it).

As for symlinks, well... This would complicate the code quite a bit and I'm not sure there's really a point to warn about it.
msg148826 - (view) Author: Julian Berman (Julian) * Date: 2011-12-04 00:24
I know this is a feature, and on occasion as pointed out a useful one. But in some cases it can be a tad annoying as the OP probably considered it.

I had a recent example where a lingering .pyc made my test suite pass (and me nearly push broken code) despite the fact that I'd moved the file away and edited it, with bugs. Obviously I now remember to rebuild and remove all .pyc's before I commit, but it took a near miss to remind me to.

Would it be useful to have a command line flag that did something like this (perhaps that test runners could set by default) to prevent others from running into similar situations?
msg148831 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-12-04 03:59
In Python 3.2 and later, standalone .pyc files are no longer created as a side effect of import. Instead, cached files are stored in a __pycache__ subdirectory and ignored completely if the corresponding source file is no longer present. (Standalone .pyc files may still be created explicitly for sourceless Python distribution)

See PEP 3147 for details.

Warnings for accidental use of standalone .pyc files will not be added to earlier versions, since there is no way for Python to tell the difference between accidental and deliberate use.
History
Date User Action Args
2022-04-11 14:57:24adminsetgithub: 57732
2011-12-04 03:59:13ncoghlansetstatus: open -> closed
resolution: out of date
messages: + msg148831

stage: resolved
2011-12-04 00:24:06Juliansetnosy: + Julian
messages: + msg148826
2011-12-03 19:49:55pitrousetnosy: + pitrou, brett.cannon, ncoghlan

messages: + msg148820
versions: + Python 3.2, Python 3.3
2011-12-03 12:27:30ezio.melottisetnosy: + ezio.melotti
messages: + msg148794
2011-12-03 12:18:57lennartyseboodtcreate