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.

Author martin.panter
Recipients brett.cannon, eric.snow, martin.panter, ncoghlan, wolma
Date 2016-07-16.08:43:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1468658583.85.0.714461935127.issue27487@psf.upfronthosting.co.za>
In-reply-to
Content
Here is a patch with the proposed warning. I think “Error finding module specification” might be a bit better than the current “finding spec”, so I included that change. With the patch, this is what the messages look like:

$ ./python -m package.module
/media/disk/home/proj/python/cpython/Lib/runpy.py:125: RuntimeWarning: 'package.module' found in sys.modules after import of any parent packages, but prior to execution; this may result in unpredictable behaviour
  warn(RuntimeWarning(msg))
/media/disk/home/proj/python/cpython/python: Error while finding module specification for 'package.module' (ValueError: package.module.__spec__ is not set)
[Exit 1]

The warning also occurs when running toplevel modules that are already imported:

$ ./python -m runpy
/media/disk/home/proj/python/cpython/Lib/runpy.py:125: RuntimeWarning: 'runpy' found in sys.modules after import of any parent packages, but prior to execution; this may result in unpredictable behaviour
  warn(RuntimeWarning(msg))
No module specified for execution

FWIW, to fully avoid the regression and keep my original bug fixed at the same time, I think we would need a way for importlib.util.find_spec() to differentiate its own exceptions (the ValueError for missing __spec__, the ImportError for nonexistent parent package, etc) from exceptions raised by __init__.py code. The exceptions currently raised by find_spec() are not flexible enough for the runpy use case.
History
Date User Action Args
2016-07-16 08:43:04martin.pantersetrecipients: + martin.panter, brett.cannon, ncoghlan, eric.snow, wolma
2016-07-16 08:43:03martin.pantersetmessageid: <1468658583.85.0.714461935127.issue27487@psf.upfronthosting.co.za>
2016-07-16 08:43:03martin.panterlinkissue27487 messages
2016-07-16 08:43:03martin.pantercreate