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

Run interpreter discovery under -I mode #2552

Merged
merged 1 commit into from
Mar 20, 2024
Merged

Conversation

charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Mar 19, 2024

Summary

If you have a file typing.py in the current working directory, python -m doesn't work in some Python versions:

❯ python -m foo
Could not import runpy module
Traceback (most recent call last):
  File "/Users/crmarsh/.local/share/rtx/installs/python/3.9.18/lib/python3.9/runpy.py", line 15, in <module>
    import importlib.util
  File "/Users/crmarsh/.local/share/rtx/installs/python/3.9.18/lib/python3.9/importlib/util.py", line 2, in <module>
    from . import abc
  File "/Users/crmarsh/.local/share/rtx/installs/python/3.9.18/lib/python3.9/importlib/abc.py", line 17, in <module>
    from typing import Protocol, runtime_checkable
ImportError: cannot import name 'Protocol' from 'typing' (/Users/crmarsh/workspace/uv/typing.py)

This did not cause problems for us on Python 3.11 or later, because we set PYTHONSAFEPATH, which avoids adding the current working directory to sys.path. However, on earlier versions, we were failing with the above. (It's important that we run interpreter discovery in the current working directory, since doing otherwise breaks pyenv shims.)

The fix implemented here uses -I to run Python in isolated mode, which is even stricter. The downside of isolated mode is that we currently rely on setting PYTHONPATH to find the "fake module" that we create on disk, and -I means PYTHONPATH is totally ignored. So, instead, we run a script directly, and that script injects the path we care about into PYTHONSAFEPATH.

Closes #2547.

@charliermarsh charliermarsh marked this pull request as ready for review March 19, 2024 19:55
@charliermarsh charliermarsh added the bug Something isn't working label Mar 19, 2024
@charliermarsh charliermarsh force-pushed the charlie/isolated branch 3 times, most recently from 24d6de5 to 061cc54 Compare March 19, 2024 21:12
@charliermarsh
Copy link
Member Author

I don't understand why this broke the Windows shims.

@charliermarsh
Copy link
Member Author

I guess the bat doesn't like a multi-line script argument... Very weird.

@charliermarsh charliermarsh changed the title Run interpreter discovery under -I mode Run interpreter discovery under -I mode Mar 19, 2024
@charliermarsh
Copy link
Member Author

I also manually verified that the Pyenv shims continue to work:

DEBUG Starting interpreter discovery for default Python
DEBUG Probing interpreter info for: /Users/crmarsh/.pyenv/shims/python3
DEBUG Found Python 3.10.2 for: /Users/crmarsh/.pyenv/shims/python3
Using Python 3.10.2 interpreter at: /Users/crmarsh/.pyenv/versions/3.10.2/bin/python3
Creating virtualenv at: .venv
Activate with: source .venv/bin/activate

Copy link
Member

@zanieb zanieb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Why -c (and previously -m) instead of just python <path>?

@charliermarsh
Copy link
Member Author

We need to be able to resolve relative imports (so we can't use python <path>), but we can't use -m because the module we want to run isn't in sys.path until we modify it. So complicated.

@charliermarsh charliermarsh merged commit c180fed into main Mar 20, 2024
31 checks passed
@charliermarsh charliermarsh deleted the charlie/isolated branch March 20, 2024 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

py3.9 and 3.10 error if typing is importable from cwd
2 participants