Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stdlib packages import local packages from working dir instead of other stdlib packages #101210

Closed
krfricke opened this issue Jan 21, 2023 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@krfricke
Copy link

Bug 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
@hauntsaninja
Copy link
Contributor

hauntsaninja commented Jan 21, 2023

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jan 21, 2023
@eryksun
Copy link
Contributor

eryksun commented 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
Copy link
Author

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!

krfricke added a commit to ray-project/ray that referenced this issue Jan 24, 2023
Python adds the current working directory as the first entry to `sys.path` . In some python versions, this will lead stdlib libraries to import modules in the working directory instead of other stdlib modules (python/cpython#101210). 

In our case, the conflict is `types.py`. Because `types.py` contains a PublicAPI we don't want to just rename it without proper deprecation. Instead we fix the `sys.path` in `setup-dev.py` as this is the only location where the problem predictably comes up.

Signed-off-by: Kai Fricke <kai@anyscale.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants