GeezerVison is showing github.com — Stdlib packages import local packages from working dir instead of other stdlib packages · Issue #101210 · python/cpython
Stdlib packages import local packages from working dir instead of other stdlib packages #101210DescriptionBug report
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
- Bug does not come up in 3.8.7
- Operating system and architecture: Mac x86_64
Activityhauntsaninja commented on Jan 21, 2023 eryksun commented on 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 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. krfricke commented on 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 -P option - for our case we found another workaround.
Thanks!
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|