[issue26099] site ignores ImportError when running sitecustomize and usercustomize

STINNER Victor report at bugs.python.org
Mon Jan 18 04:12:29 EST 2016


STINNER Victor added the comment:

Example of the bug:

----
$ cat Lib/sitecustomize.py
print("before")
import xxx
print("after")

haypo at smithers$ ./python -q -c pass
before
----

The line "after" is not executed and *no* error nor warning is raised. With the patch:

----
$ ./python -q -c pass
before
Error in sitecustomize; set PYTHONVERBOSE for traceback:
ImportError: No module named 'xxx'

$ PYTHONVERBOSE=1 ./python -q -c pass
(...)
# /home/haypo/prog/python/3.5/Lib/__pycache__/sitecustomize.cpython-35.pyc matches /home/haypo/prog/python/3.5/Lib/sitecustomize.py
# code object from '/home/haypo/prog/python/3.5/Lib/__pycache__/sitecustomize.cpython-35.pyc'
before
Traceback (most recent call last):
  File "/home/haypo/prog/python/3.5/Lib/site.py", line 508, in execsitecustomize
    import sitecustomize
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/haypo/prog/python/3.5/Lib/sitecustomize.py", line 2, in <module>
    import xxx
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'xxx'
# destroy sitecustomize
(...)
----

Brett Cannon: "This is a change in semantics."

Can you please elaborate? Even if importing sitecustomize changes, Python continue its execution. Only a warning is emited.

Brett Cannon: "It might be better to log an ImportWarning when the import fails and keep the current semantics (and be careful about importing warnings)."

With my patch, a message "Error in sitecustomize; set PYTHONVERBOSE for traceback:" is logged. I used the same behaviour then the code to handle site import.

Note: I found this issue when I worked on the PEP 511 to register a code transformer at startup. The bug is really annoying: the code transformers may or may not be registered depending if required modules can be important, I expect an error (or at least a warning).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26099>
_______________________________________


More information about the Python-bugs-list mailing list