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 BreamoreBoy, eric.araujo, eric.snow, martin.panter, ncoghlan, robagar, schlamar
Date 2015-02-21.09:07:41
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1424509661.7.0.21958709745.issue14285@psf.upfronthosting.co.za>
In-reply-to
Content
The relevant code is in the _get_module_details() function at Lib/runpy.py:101. There are a few of things going on:

1. The code is calling importlib.util.find_spec("<package>.__main__"), and handles various kinds of exceptions by wrapping them in an ImportError, adding the “Error while finding spec” message. The call apparently causes the package to be imported, so some exceptions triggered by running __init__.py are wrapped. It would be nice if exceptions raised by running __init__.py were not caught, but I have no idea if this is practical or how to do it. It seems the exception handler is there to raise ImportError if you do something like “python -m os.path.something”, which seems rather unlikely to me.

2. The logic for handling the __main__ module in a package seems to assume that any ImportError (such as raised from step 1) is due to the __main__ module not being present. Then it wraps that exception in another ImportError, adding the “. . . cannot be directly executed” bit. Again, it would be good to separate the __init__.py running from this exception handling.

3. Finally in _run_module_as_main(), the ImportError is caught and printed, without any traceback. It would be good to only do this for internal errors loading the module, and not if ImportError is raised when the module (or __init__.py) is run.

I’m not sure what the best way to fix this is. Perhaps add an internal ImportError subclass (or subclasses) that is only used for the “No module named . . .” errors, or only used for errors appropriate for final handler in step 3.
History
Date User Action Args
2015-02-21 09:07:41martin.pantersetrecipients: + martin.panter, ncoghlan, eric.araujo, BreamoreBoy, schlamar, eric.snow, robagar
2015-02-21 09:07:41martin.pantersetmessageid: <1424509661.7.0.21958709745.issue14285@psf.upfronthosting.co.za>
2015-02-21 09:07:41martin.panterlinkissue14285 messages
2015-02-21 09:07:41martin.pantercreate