Skip to content

Files in local directory can shadow stdlib when running PDM#3775

Description

@ncoghlan

Describe the bug

Working on demos for a talk about diagnosing import system failures, I inadvertently broke PDM by creating an re.py file:

$ pdm sync
All packages are synced to date, nothing to do.

  0:00:00 馃帀 All complete! 0/0
$ touch re.py
$ pdm sync
INFO: The saved Python interpreter does not exist or broken. Trying to find another one.
WARNING: Project requires a python version of >=3.14, The virtualenv is being created for you as it cannot be matched to the right version.
INFO: python.use_venv is on, creating a virtualenv for this project...
ERROR: Failed to install Python cpython@3.14.2: Command '['/home/ncoghlan/.local/share/pdm/python/cpython@3.14.2/bin/python3', '-EsSc', 'import platform; 
print(platform.python_implementation().lower())']' returned non-zero exit status 1.
[VirtualenvCreateError]: Can't resolve python interpreter

Normal startup won't be adding the current directory to sys.path (since I'm running the pdm wrapper script, the implicitly added sys.path[0] directory will be the local binaries folder), so there's presumably some sys.path manipulation in pdm that is adding it back.

To reproduce

Create an re.py (for example) file in the project directory.

Expected Behavior

Current directory does not end up in sys.path when running PDM commands

Environment Information

$ PYTHONPATH= python -Pm site
sys.path = [
    '/usr/lib64/python314.zip',
    '/usr/lib64/python3.14',
    '/usr/lib64/python3.14/lib-dynload',
    '/home/ncoghlan/.local/lib/python3.14/site-packages',
    '/usr/lib64/python3.14/site-packages',
    '/usr/lib/python3.14/site-packages',
]
USER_BASE: '/home/ncoghlan/.local' (exists)
USER_SITE: '/home/ncoghlan/.local/lib/python3.14/site-packages' (exists)
ENABLE_USER_SITE: True
[ncoghlan@thechalk fwiw]$ PYTHONPATH=: python -Pm site
sys.path = [
    '/home/ncoghlan/devel/fwiw',
    '/usr/lib64/python314.zip',
    '/usr/lib64/python3.14',
    '/usr/lib64/python3.14/lib-dynload',
    '/home/ncoghlan/.local/lib/python3.14/site-packages',
    '/usr/lib64/python3.14/site-packages',
    '/usr/lib/python3.14/site-packages',
]
USER_BASE: '/home/ncoghlan/.local' (exists)
USER_SITE: '/home/ncoghlan/.local/lib/python3.14/site-packages' (exists)
ENABLE_USER_SITE: True

Verbose Command Output

No response

Additional Context

From a quick review, one potential culprit might be the way an empty PYTHONPATH is handled: pdm adds an extra separator to the PYTHONPATH entry it defined in that case, and python interprets a trailing separator differently from the entry being entirely blank:

Blank PYTHONPATH, current directory not added:

$ PYTHONPATH= python -Pm site
sys.path = [
    '/usr/lib64/python314.zip',
    '/usr/lib64/python3.14',
    '/usr/lib64/python3.14/lib-dynload',
    '/home/ncoghlan/.local/lib/python3.14/site-packages',
    '/usr/lib64/python3.14/site-packages',
    '/usr/lib/python3.14/site-packages',
]
USER_BASE: '/home/ncoghlan/.local' (exists)
USER_SITE: '/home/ncoghlan/.local/lib/python3.14/site-packages' (exists)
ENABLE_USER_SITE: True

PYTHONPATH with trailing path separator, current directory added (as "" in sys.path is equivalent to "."):

$ PYTHONPATH=: python -Pm site
sys.path = [
    '/home/ncoghlan/devel/fwiw',
    '/usr/lib64/python314.zip',
    '/usr/lib64/python3.14',
    '/usr/lib64/python3.14/lib-dynload',
    '/home/ncoghlan/.local/lib/python3.14/site-packages',
    '/usr/lib64/python3.14/site-packages',
    '/usr/lib/python3.14/site-packages',
]
USER_BASE: '/home/ncoghlan/.local' (exists)
USER_SITE: '/home/ncoghlan/.local/lib/python3.14/site-packages' (exists)
ENABLE_USER_SITE: True

(That shouldn't be the case for me, as I don't have PYTHONPATH set at all, so this isn't a definitive culprit. #3746 could also potentially be related)

Are you willing to submit a PR to fix this bug?

  • Yes, I would like to submit a PR.

Activity

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

    馃悰 bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions