You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Stdlib packages import local packages from working dir instead of other stdlib packages #101210
Global packages (e.g. enum) try to import from local packages in the current working dir, even if a same-named stdlib package exists.
For instance, if a types.py exists in the local directory, import enum will fail:
> touch types.py
> python
Python 3.10.9 (main, Jan 20 2023, 17:02:49) [Clang 13.0.0 (clang-1300.0.27.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/kai/.pyenv/versions/3.10.9/lib/python3.10/enum.py", line 2, in <module>
from types import MappingProxyType, DynamicClassAttribute
ImportError: cannot import name 'MappingProxyType' from 'types' (/home/kai/sandbox/types.py)
The correct package to import should be the stdlib types.py package, not the local types.py.
Your environment
CPython versions tested on: Python 3.10.4, 3.10.9, 3.8.6
When running interactively, an empty string is prepended to sys.path. This gets evaluated to whatever the current working directory is at the time of each import. When running a script, the directory of the script gets prepended to sys.path. Usually the script directory is not the same as the current working directory.
You can run in isolated mode (i.e. -I command-line option) to avoid prepending either the working directory or the script directory. However, this is an extreme measure that also disables reading PYTHON* environment variables and the user site packages. Starting with Python 3.11, there's a -P command-line option to get a safe sys.path that doesn't prepend these directories.
Activity
hauntsaninja commentedon Jan 21, 2023
Duplicate of #95893, #90482, #42852, #73790, #66368, #65577, #65401, etc
See https://ncoghlan-devs-python-notes.readthedocs.io/en/latest/python_concepts/import_traps.html#the-name-shadowing-trap
eryksun commentedon Jan 21, 2023
When running interactively, an empty string is prepended to
sys.path. This gets evaluated to whatever the current working directory is at the time of each import. When running a script, the directory of the script gets prepended tosys.path. Usually the script directory is not the same as the current working directory.You can run in isolated mode (i.e.
-Icommand-line option) to avoid prepending either the working directory or the script directory. However, this is an extreme measure that also disables readingPYTHON*environment variables and the user site packages. Starting with Python 3.11, there's a-Pcommand-line option to get a safesys.paththat doesn't prepend these directories.krfricke commentedon Jan 21, 2023
Thanks for the explanation and sorry for the duplicate (I tried searching but didn't find the other issues).
Looking forward to the
-Poption - for our case we found another workaround.Thanks!
Fix setup-dev.py stdlib import shadowing (#31829)
tracebackmodule leads to unexpected code execution #138170 as a duplicate of this issue