GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #101210

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
Stdlib packages import local packages from working dir instead of other stdlib packages#101210
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@krfricke
krfricke
opened on Jan 21, 2023
Issue body actions

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

Activity

  1. krfricke
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jan 21, 2023
  2. hauntsaninja commented on Jan 21, 2023

    @hauntsaninja
    hauntsaninja
    on Jan 21, 2023
    Contributor
    More actions

    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

  3. hauntsaninja
    closed this as not plannedon Jan 21, 2023
  4. eryksun commented on Jan 21, 2023

    @eryksun
    eryksun
    on Jan 21, 2023
    Contributor
    More actions

    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.

  5. krfricke commented on Jan 21, 2023

    @krfricke
    krfricke
    on Jan 21, 2023
    Author
    More actions

    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!

  6. krfricke
    added a commit that references this issue on Jan 24, 2023

    Fix setup-dev.py stdlib import shadowing (#31829)

    3e1cdeb
  7. picnixz
    marked shadowing error with traceback module leads to unexpected code execution #138170 as a duplicate of this issue on Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.