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

Update activation scripts from virtualenv #3376

Merged
merged 2 commits into from
May 4, 2024
Merged

Conversation

charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented May 4, 2024

Summary

Refreshes some of the activation scripts, and fixes some bugs in activate_this.py that were likely the rest of some erroneous copy-pasting.

Closes #3346.

Test Plan

❯ python
Python 3.12.0 (main, Feb 28 2024, 09:44:16) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import httpx
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'httpx'
>>> activator = '.venv/bin/activate_this.py'
>>> with open(activator) as f:
...     exec(f.read(), {'__file__': activator})
...
>>> import httpx

@charliermarsh charliermarsh requested a review from konstin May 4, 2024 21:48
@charliermarsh charliermarsh added the bug Something isn't working label May 4, 2024
@charliermarsh charliermarsh marked this pull request as ready for review May 4, 2024 21:48
let relative_site_packages = [
interpreter.virtualenv().purelib.as_path(),
interpreter.virtualenv().platlib.as_path(),
]
Copy link
Member Author

Choose a reason for hiding this comment

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

Technically need to include both, though we dedup just below.


# add the virtual environments libraries to the host python import mechanism
prev_length = len(sys.path)
for lib in "{{ RELATIVE_SITE_PACKAGES }}".split(os.pathsep):
path = os.path.realpath(os.path.join(bin_dir, lib))
site.addsitedir(path.decode("utf-8") if "" else path)
site.addsitedir(path)
sys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length]

sys.real_prefix = sys.prefix
Copy link
Member

@AlexWaygood AlexWaygood May 4, 2024

Choose a reason for hiding this comment

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

I know this is what virtualenv has done for a long time, and that this script is largely vendored from virtualenv, but it's very strange to me that this script monkeypatches a variable onto the sys module that wouldn't exist otherwise. I don't think the stdlib venv module does this, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

I haven't dug into the history deeply, but I think it's a legacy virtualenv thing, possibly from before various evolutions to the sys module came about? We don't rely on this anywhere (though pip and others have code to detect it). I'm inclined to just leave it as-is to avoid deviations, it doesn't really cost us anything.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah. While it's still very weird to me that a script of ours would do this, I don't mind it too much given that virtualenv has been doing it since forever.

@charliermarsh charliermarsh enabled auto-merge (squash) May 4, 2024 23:21
@charliermarsh charliermarsh merged commit 37635fd into main May 4, 2024
43 checks passed
@charliermarsh charliermarsh deleted the charlie/activate branch May 4, 2024 23:30
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.

Bugs in the activate_this.py script created by uv venv
2 participants