Issue 26099: site ignores ImportError when running sitecustomize and usercustomize (original) (raw)
Example of the bug:
$ cat Lib/sitecustomize.py print("before") import xxx print("after")
haypo@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 "", line 969, in _find_and_load File "", line 958, in _find_and_load_unlocked File "", line 673, in _load_unlocked File "", line 662, in exec_module File "", line 222, in _call_with_frames_removed File "/home/haypo/prog/python/3.5/Lib/sitecustomize.py", line 2, in import xxx File "", line 969, in _find_and_load File "", 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).