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

Automatically detect virtual environments when used via python -m uv #1504

Merged
merged 2 commits into from
Feb 16, 2024

Conversation

zanieb
Copy link
Member

@zanieb zanieb commented Feb 16, 2024

Closes #1501

@zanieb
Copy link
Member Author

zanieb commented Feb 16, 2024

cc @altendky :)

return value

# Otherwise, check if we're in a venv
maybe_venv_dir = os.path.dirname(os.path.dirname(sys.executable))
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be something like (Path(sys.prefix) / "pyvenv.cfg").exists() (probably without the pathlib for speed, but I'm lazy here)? This assumes that there is exactly one level between the bin or Scripts dir and the base prefix, which I don't think is guaranteed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! I thought what I was doing might be brittle.. let me look into that.

Choose a reason for hiding this comment

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

https://docs.python.org/3/library/sysconfig.html#installation-paths Looks like you can sometimes get a posix_prefix. Might lead to more details about 'correctness' as well.

@altendky
Copy link

Thanks!

$ python3.11 -m venv venv
$ venv/bin/python -m pip install 'uv @ git+https://github.com/astral-sh/uv@1ed6ba0ba0d3756312085ec21c90469985b5bbb6'
Collecting uv@ git+https://github.com/astral-sh/uv@1ed6ba0ba0d3756312085ec21c90469985b5bbb6
  Using cached uv-0.1.2-py3-none-linux_x86_64.whl
Installing collected packages: uv
Successfully installed uv-0.1.2

[notice] A new release of pip is available: 23.2.1 -> 24.0
[notice] To update, run: /home/altendky/tmp/venv/bin/python -m pip install --upgrade pip

Installed main above and see it failing below.

$ venv/bin/python -m uv pip install attrs
error: Failed to locate a virtualenv or Conda environment (checked: `VIRTUAL_ENV`, `CONDA_PREFIX`, and `.venv`). Run `uv venv` to create a virtualenv.
$ venv/bin/python -m pip install 'uv @ git+https://github.com/astral-sh/uv@967188a914df3461b92de27e9b9234fd8c22be85'
Collecting uv@ git+https://github.com/astral-sh/uv@967188a914df3461b92de27e9b9234fd8c22be85
  Using cached uv-0.1.1-py3-none-linux_x86_64.whl
Installing collected packages: uv
  Attempting uninstall: uv
    Found existing installation: uv 0.1.2
    Uninstalling uv-0.1.2:
      Successfully uninstalled uv-0.1.2
Successfully installed uv-0.1.1

[notice] A new release of pip is available: 23.2.1 -> 24.0
[notice] To update, run: /home/altendky/tmp/venv/bin/python -m pip install --upgrade pip

Installed this PR above and see it succeeding below.

$ venv/bin/python -m uv pip install attrs
Resolved 1 package in 107ms
Installed 1 package in 9ms
 + attrs==23.2.0

@zanieb zanieb merged commit f87c29e into main Feb 16, 2024
7 checks passed
@zanieb zanieb deleted the zb/venv-python branch February 16, 2024 18:55
zanieb added a commit that referenced this pull request Feb 18, 2024
…#1667)

From #1623 (comment)

I thought I checked this was working correctly in #1504 but I guess the
environment is not preserved like I thought.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

venv must be activated even when running via venv/bin/python -m uv
3 participants