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

Install and manage Python interpreters #2607

Closed
axiomofjoy opened this issue Mar 22, 2024 · 22 comments
Closed

Install and manage Python interpreters #2607

axiomofjoy opened this issue Mar 22, 2024 · 22 comments
Assignees
Labels
enhancement New feature or request projects Related to project management capabilities

Comments

@axiomofjoy
Copy link

uv venv accepts a --python flag that allows the user to specify a path to a particular Python interpreter or version if it is already installed on the machine. I would like for uv to install and manage Python for me, similar to conda create --name env-name python=3.9.

I am really enjoying using uv so far. Keep up the good work!

@zanieb
Copy link
Member

zanieb commented Mar 22, 2024

This is on our roadmap :)

@zanieb zanieb added enhancement New feature or request projects Related to project management capabilities labels Mar 22, 2024
@zanieb zanieb self-assigned this Mar 22, 2024
@dillfrescott
Copy link

Also looking forward to this!

@zanieb zanieb changed the title install and manage python interpreter Install and manage Python interpreters Apr 19, 2024
@Spenhouet
Copy link

Consider the possibility to specify the required python version via a .python-version file within the project root dir, as suggested here: #1850

@mlissner
Copy link

I just want to chime in to say that I'm evaluating uv right now and my big question was whether and how it would handle installing Python itself. This is embarrassing, but I've been a Python developer for over a decade. I've used pip, virtualenvwrapper, Python 2 and 3, poetry, and more.

Whenever somebody tells me I have to use a specific version of Python, it sucks the life right out of me. I know there are tools for this. I know I use pip or poetry. I know I'm going to lose an hour figuring it all out, just like last time....

So, from my perspective, as a long-time Python dev, but an occasional installer of Python, I'd love to see this!

Thank you for the great tool. Impressive stuff.

@zanieb
Copy link
Member

zanieb commented Apr 24, 2024

We agree it's awfully painful to install and manage multiple Python versions across platforms. We'll be tackling this problem soon.

@Oppen
Copy link

Oppen commented May 21, 2024

In terms of roadmap, is there any particular release that should have it or is it still the indeterminate future? I'd love to get rid of pyenv and need to set appropriate expectations 👀

@zanieb
Copy link
Member

zanieb commented May 21, 2024

It's next on my list of projects, following tool management (#3560) and rewriting interpreter discovery (#3266)

@gotounix
Copy link

gotounix commented Jun 5, 2024

@zanieb It's been another half month, how's the progress going, is there a preview version?

zanieb added a commit that referenced this issue Jun 7, 2024
In preparation for managed toolchains #2607, just renames the crate to
something broader.

See #4121 and #4138 to see the final
intent.
zanieb added a commit that referenced this issue Jun 7, 2024
Extends #4120 
Part of #2607 

There should be no behavior changes here. Restructures the discovery API
to be focused on a toolchain first perspective in preparation for
exposing a `find_or_fetch` method for toolchains in
#4138.
zanieb added a commit that referenced this issue Jun 10, 2024
…4138)

Extends #4121
Part of #2607 

Adds support for managed toolchain fetching to `uv venv`, e.g.

```
❯ cargo run -q -- venv --python 3.9.18 --preview -v
DEBUG Searching for Python 3.9.18 in search path or managed toolchains
DEBUG Searching for managed toolchains at `/Users/zb/Library/Application Support/uv/toolchains`
DEBUG Found CPython 3.12.3 at `/opt/homebrew/bin/python3` (search path)
DEBUG Found CPython 3.9.6 at `/usr/bin/python3` (search path)
DEBUG Found CPython 3.12.3 at `/opt/homebrew/bin/python3` (search path)
DEBUG Requested Python not found, checking for available download...
DEBUG Using registry request timeout of 30s
INFO Fetching requested toolchain...
DEBUG Downloading https://github.com/indygreg/python-build-standalone/releases/download/20240224/cpython-3.9.18%2B20240224-aarch64-apple-darwin-pgo%2Blto-full.tar.zst to temporary location /Users/zb/Library/Application Support/uv/toolchains/.tmpgohKwp
DEBUG Extracting cpython-3.9.18%2B20240224-aarch64-apple-darwin-pgo%2Blto-full.tar.zst
DEBUG Moving /Users/zb/Library/Application Support/uv/toolchains/.tmpgohKwp/python to /Users/zb/Library/Application Support/uv/toolchains/cpython-3.9.18-macos-aarch64-none
Using Python 3.9.18 interpreter at: /Users/zb/Library/Application Support/uv/toolchains/cpython-3.9.18-macos-aarch64-none/install/bin/python3
Creating virtualenv at: .venv
INFO Removing existing directory
Activate with: source .venv/bin/activate
```

The preview flag is required. The fetch is performed if we can't find an
interpreter that satisfies the request. Once fetched, the toolchain will
be available for later invocations that include the `--preview` flag.
There will be follow-ups to improve toolchain management in general,
there is still outstanding work from the initial implementation.
@zanieb
Copy link
Member

zanieb commented Jun 10, 2024

Hi! There's now "preview" support for fetching and managing Python toolchains for you. I'll be working on documentation for the feature this week and there's quite a bit of work to be done before it's production-ready, but uv venv --preview --python <version> can now download missing versions for you.

@axiomofjoy
Copy link
Author

Awesome @zanieb, excited to try it out!

@Oppen
Copy link

Oppen commented Jun 10, 2024

You say "preview", I read "already in prod 😎". Jk, but I'm gonna use it on my personal box and see how it goes ❤️

@gotounix
Copy link

gotounix commented Jun 11, 2024

@zanieb It's awesome, I tried it out, and everything works fine.
But I found it required py launch while using py --list-paths to find Python installations.

$ cargo run -q -- venv --python 3.10.11 --preview -v
warning: `C:\Users\Test\.cargo\config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
DEBUG Searching for Python 3.10.11 in search path, `py` launcher output, or managed toolchains
DEBUG Searching for managed toolchains at `C:\Users\Test\AppData\Roaming\uv\data\toolchains`
  x The `py` launcher could not be found.

Is it possible to use a --standalone parameter to manage standalone Python downloads. And while using --standalone it won't using py --list-paths to find Python installtions. In this way, uv will create venv by using standalone Python without py launch.

@zanieb
Copy link
Member

zanieb commented Jun 11, 2024

@gotounix there's a UV_FORCE_MANAGED_PYTHON environment variable that, if set, ignores other interpreters but it's not tested and we'll be removing that in favor of whatever design we settle on in #4198 .

@gotounix
Copy link

gotounix commented Jun 19, 2024

@gotounix there's a UV_FORCE_MANAGED_PYTHON environment variable that, if set, ignores other interpreters but it's not tested and we'll be removing that in favor of whatever design we settle on in #4198 .

Is there a way to put the UV_FORCE_MANAGED_PYTHON environment variable in a configuration file so that it doesn't have to be entered every time?

@zanieb
Copy link
Member

zanieb commented Jun 19, 2024

@gotounix that'll be included in #4198

@gotounix
Copy link

gotounix commented Jul 8, 2024

0.2.22 version can use this command to create venv:

uv venv --python 3.10.11 --python-preference only-managed

@zanieb
Copy link
Member

zanieb commented Jul 8, 2024

We basically do this now, so I'm going to close this issue. We plan to add python shim support in the future.

@zanieb zanieb closed this as completed Jul 8, 2024
@hyperknot
Copy link

hyperknot commented Sep 10, 2024

This is super nice! One small point, the help is missing / incomplete for the venv part. I had to read here and try my best guess that I can actually do this line:

uv venv --python 3.12

I think this line is the best thing which has happened to the Python ecosystem in the last 10 years, please advertise it and put it in huge texts, it's amazing!

@zanieb
Copy link
Member

zanieb commented Sep 10, 2024

Thanks! There's https://docs.astral.sh/uv/guides/install-python/#viewing-python-installations maybe we could include it there. Ideally you just use uv run and don't even need to make a virtual environment yourself!

@hyperknot
Copy link

I'm always setting up a venv for every single one of my projects, and I think it's a best practice among Python devs. It's great to hear that I can get rid of pyenv + .python-version files with the new uv versions.

@gusutabopb
Copy link

@hyperknot uv creates venvs automatically. I don't think any of the higher-level commands (the stuff released in 0.3.0, i.e. everything except uv pip and uv venv) work without one.

You can make a venv manually before running uv add, uv run, etc, but you don't need to - it gets created automatically.

Example:

> uv init
Initialized project `uv-test`
> uv add cowsay
Using Python 3.12.4
Creating virtualenv at: .venv
Resolved 2 packages in 213ms
Prepared 1 package in 25ms
Installed 1 package in 5ms
 + cowsay==6.1
> uv run python -m cowsay -t hello
  _____
| hello |
  =====
     \
      \
        ^__^
        (oo)\_______
        (__)\       )\/\
            ||----w |
            ||     ||

@hyperknot
Copy link

hyperknot commented Sep 11, 2024

But I'm not getting something. I don't want to init and add, that's the easy part. I want a reproducible dev environment, which cleans up and sets everything up for me.

Here is the file I'm using in all my projects, I call this file with source or .. Every time I modify the dependencies, I just run it, it runs super fast, thanks to uv.

#!/usr/bin/env bash

find . -name "*.egg-info" -exec rm -rf {} +
find . -name __pycache__ -exec rm -rf {} +
find . -name .ipynb_checkpoints -exec rm -rf {} +
find . -name .pytest_cache -exec rm -rf {} +
find . -name .ruff_cache -exec rm -rf {} +
find . -name .DS_Store -exec rm -rf {} +


rm -rf .venv

uv venv
source .venv/bin/activate

uv pip -V
uv pip install -e .

cd js_tools
pnpm i
cd ..

+ I pair it with direnv so that it auto-activates when I'm inside the directory.

How would alternative uv commands help me in this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request projects Related to project management capabilities
Projects
None yet
Development

No branches or pull requests

9 participants