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

Set VIRTUAL_ENV in Jupyter kernels #11155

Merged
merged 2 commits into from
Feb 1, 2025
Merged

Set VIRTUAL_ENV in Jupyter kernels #11155

merged 2 commits into from
Feb 1, 2025

Conversation

charliermarsh
Copy link
Member

Summary

It turns out activating the kernel does not change VIRTUAL_ENV, so we still install into the environment the Jupyter environment, rather than the project environment.

Unfortunately, after this change, we do still show a warning on uv add:

warning: `VIRTUAL_ENV=/Users/crmarsh/.cache/uv/archive-v0/3bddKDdYXuX2w57Fu6itL` does not match the project environment path `.venv` and will be ignored

uv pip install works without warning.

Closes #11154.

@charliermarsh charliermarsh requested a review from zanieb February 1, 2025 19:43
@charliermarsh charliermarsh added the documentation Improvements or additions to documentation label Feb 1, 2025
@charliermarsh
Copy link
Member Author

@zanieb -- Do you think it's fair game to suppress that warning if UV_PYTHON is set?

@indrayam
Copy link

indrayam commented Feb 1, 2025

Thank you for this, @charliermarsh Will try it once I am home.

@charliermarsh
Copy link
Member Author

Thanks so much for pointing it out. Not sure if I was confused when I wrote the docs or if something changed in Jupyter or uv.

@zanieb
Copy link
Member

zanieb commented Feb 1, 2025

@zanieb -- Do you think it's fair game to suppress that warning if UV_PYTHON is set?

Not broadly. Like, UV_PYTHON just says which interpreter to use for creating the project environment. It doesn't change that the active environment is inequal to the project environment path.

I'll need to look more closely at what's going on here though.

@charliermarsh
Copy link
Member Author

Jupyter runs in an ephemeral environment, since it's installed via --with. The project is located at .venv. The user creates a kernel for the project. We want users to be able to run !uv add or !uv pip install. VIRTUAL_ENV is set to the ephemeral environment, despite the kernel. !uv pip install fails because it installs into the ephemeral environment (so this change sets UV_PYTHON explicitly to solve that), and !uv add succeeds but erroneously warns since VIRTUAL_ENV does not equal the project path.

@charliermarsh
Copy link
Member Author

The other option is setting VIRTUAL_ENV in the kernel spec, as opposed to UV_PYTHON. I don't know what the implications of that are.

@zanieb
Copy link
Member

zanieb commented Feb 1, 2025

I see. It seems okay to silence the VIRTUAL_ENV warning if UV_PYTHON is set to the project environment path specifically / or (as a broader solution) if the requested interpreter matches the project environment, e.g., -p .venv.

It does seem more correct to set VIRTUAL_ENV if we can.

@charliermarsh
Copy link
Member Author

We can, yeah.

@indrayam
Copy link

indrayam commented Feb 1, 2025

Thanks so much for pointing it out. Not sure if I was confused when I wrote the docs or if something changed in Jupyter or uv.

I absolutely adore "uv". And since NOTHING I do these days does not use uv, Jupyter related challenges had been bugging me. Anyways, about to try your changes. Will keep you all posted.

@danielhollas
Copy link
Contributor

Btw: the warning situation is somewhat similar to what happens when uv is run from pre-commit

astral-sh/uv-pre-commit#36

@indrayam
Copy link

indrayam commented Feb 1, 2025

Thanks so much for pointing it out. Not sure if I was confused when I wrote the docs or if something changed in Jupyter or uv.

I absolutely adore "uv". And since NOTHING I do these days does not use uv, Jupyter related challenges had been bugging me. Anyways, about to try your changes. Will keep you all posted.

I recreated a new project and created a new ipython kernel using the updated uv run ipython kernel (as per @charliermarsh's commit diff). Everything worked like a charm! And by setting the VIRTUAL_ENV environment variable at the kernel level, there were no warnings that I saw, not even when I ran the !uv add command. Not sure if that was supposed to be the case.

image

@charliermarsh
Copy link
Member Author

Awesome, thank you for testing! It's good to see that the warning is gone too.

@charliermarsh charliermarsh merged commit 2dfeafb into main Feb 1, 2025
61 checks passed
@charliermarsh charliermarsh deleted the charlie/jupyter branch February 1, 2025 21:54
@zanieb zanieb changed the title Set UV_PYTHON in Jupyter kernels Set VIRTUAL_ENV in Jupyter kernels Feb 1, 2025
@indrayam
Copy link

indrayam commented Feb 1, 2025

Thanks so much for pointing it out. Not sure if I was confused when I wrote the docs or if something changed in Jupyter or uv.

I absolutely adore "uv". And since NOTHING I do these days does not use uv, Jupyter related challenges had been bugging me. Anyways, about to try your changes. Will keep you all posted.

I recreated a new project and created a new ipython kernel using the updated uv run ipython kernel (as per @charliermarsh's commit diff). Everything worked like a charm! And by setting the VIRTUAL_ENV environment variable at the kernel level, there were no warnings that I saw, not even when I ran the !uv add command. Not sure if that was supposed to be the case.
...

Btw, on a somewhat related note, I am not entirely clear how uv run --with jupyter jupyter notebook invocations at times creates a new folder inside $HOME/.cache/uv/archive-v0/.. with all the jupyter related dependencies and at other times reuses the existing one.

Last evening, as I was dabbling with this, I saw uv run --with jupyter... create a folder under .../archive-v0/. This morning, when I decided to rerun the same command as I continued my work, I noticed it created a brand new folder and redownloaded Jupyter. Moreover, the older folder was not there anymore.

I understand that since it is inside $HOME/.cache/uv/.., it is ephemeral. I was just curious, especially about the logic you are using to clear the existing Jupyter-related dependencies folder from the cache.

tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Feb 4, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [astral-sh/uv](https://github.com/astral-sh/uv) | patch | `0.5.25` -> `0.5.27` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>astral-sh/uv (astral-sh/uv)</summary>

### [`v0.5.27`](https://github.com/astral-sh/uv/blob/HEAD/CHANGELOG.md#0527)

[Compare Source](astral-sh/uv@0.5.26...0.5.27)

##### Enhancements

-   Avoid setting permissions during tar extraction ([#&#8203;11191](astral-sh/uv#11191))
-   Remove warnings for missing lower bounds ([#&#8203;11195](astral-sh/uv#11195))
-   Update PubGrub to set-based outdated priority tracking ([#&#8203;11169](astral-sh/uv#11169))
-   Improve error messages for `uv pip install` with `--extra` or `--all-extras` and invalid sources ([#&#8203;11193](astral-sh/uv#11193))
-   Sign Docker images using GitHub attestations ([#&#8203;8685](astral-sh/uv#8685))

##### Preview features

-   Don't expand self-referential extras in the build backend ([#&#8203;11142](astral-sh/uv#11142))

##### Performance

-   Filter discovered Python executables by source before querying ([#&#8203;11143](astral-sh/uv#11143))
-   Optimize exclusion computation for markers ([#&#8203;11158](astral-sh/uv#11158))
-   Use Astral-maintained `tokio-tar` fork ([#&#8203;11174](astral-sh/uv#11174))
-   Remove unneeded `.clone()` ([#&#8203;11127](astral-sh/uv#11127))

##### Bug fixes

-   Fix relative paths in bytecode compilation ([#&#8203;11177](astral-sh/uv#11177))
-   Percent-decode URLs in canonical comparisons ([#&#8203;11088](astral-sh/uv#11088))
-   Respect concurrency limits in parallel index fetch ([#&#8203;11182](astral-sh/uv#11182))
-   Use wire JSON schema for conflict items ([#&#8203;11196](astral-sh/uv#11196))
-   Use explicit `_GLibCVersion` tuple in uv-python crate ([#&#8203;11122](astral-sh/uv#11122))

##### Documentation

-   Add Git SHA locking behavior to docs ([#&#8203;11125](astral-sh/uv#11125))
-   Add best-practice flags to `pip install` example in troubleshooting guide ([#&#8203;11194](astral-sh/uv#11194))
-   Set `VIRTUAL_ENV` in Jupyter kernels ([#&#8203;11155](astral-sh/uv#11155))
-   Add instructions for deactivating an environment ([#&#8203;11200](astral-sh/uv#11200))

### [`v0.5.26`](https://github.com/astral-sh/uv/blob/HEAD/CHANGELOG.md#0526)

[Compare Source](astral-sh/uv@0.5.25...0.5.26)

##### Enhancements

-   Add support for `uvx python` ([#&#8203;11076](astral-sh/uv#11076))
-   Allow `--no-dev --invert` in `uv tree` ([#&#8203;11068](astral-sh/uv#11068))
-   Update `uv python install --reinstall` to reinstall all previous versions ([#&#8203;11072](astral-sh/uv#11072))
-   Consistently write log messages with capitalized first word ([#&#8203;11111](astral-sh/uv#11111))
-   Suggest `--build-backend` when `--backend` is passed to `uv init` ([#&#8203;10958](astral-sh/uv#10958))
-   Improve retry trace message ([#&#8203;11108](astral-sh/uv#11108))

##### Performance

-   Remove unnecessary UTF-8 conversion in hash parsing ([#&#8203;11110](astral-sh/uv#11110))

##### Bug fixes

-   Ignore non-hash fragments in HTML API responses ([#&#8203;11107](astral-sh/uv#11107))
-   Avoid resolving symbolic links when querying Python interpreters ([#&#8203;11083](astral-sh/uv#11083))
-   Avoid sharing state between universal and non-universal resolves ([#&#8203;11051](astral-sh/uv#11051))
-   Error when `--script` is passing a non-PEP 723 script ([#&#8203;11118](astral-sh/uv#11118))
-   Make metadata deserialization failures non-fatal in the cache ([#&#8203;11105](astral-sh/uv#11105))
-   Mark metadata as dynamic when reading from built wheel cache ([#&#8203;11046](astral-sh/uv#11046))
-   Propagate credentials for `<index>/simple` to `<index>/...` endpoints ([#&#8203;11074](astral-sh/uv#11074))
-   Fix conflicting extra bug during `uv sync` ([#&#8203;11075](astral-sh/uv#11075))

##### Documentation

-   Add PyTorch XPU instructions to the PyTorch guide ([#&#8203;11109](astral-sh/uv#11109))
-   Add docs for signal handling ([#&#8203;11041](astral-sh/uv#11041))
-   Explain build frontend vs. build backend ([#&#8203;11094](astral-sh/uv#11094))
-   Fix formatting of `RUST_LOG` documentation ([#&#8203;10053](astral-sh/uv#10053))
-   Fix typo in `--no-deps` description ([#&#8203;11073](astral-sh/uv#11073))
-   Reflow CLI documentation comments ([#&#8203;11040](astral-sh/uv#11040))
-   Shorten "Using existing Python versions" nav item so it fits on one line ([#&#8203;11077](astral-sh/uv#11077))
-   Some minor touch-ups to the Python install guide ([#&#8203;11116](astral-sh/uv#11116))
-   Update Dependabot tracking issue link ([#&#8203;11054](astral-sh/uv#11054))
-   Update documentation for running in a container ([#&#8203;11052](astral-sh/uv#11052))
-   Upgrade PyTorch version in documentation ([#&#8203;11114](astral-sh/uv#11114))
-   Use `sys_platform` in lieu of `platform_system` in PyTorch docs ([#&#8203;11113](astral-sh/uv#11113))
-   Use positive (rather than negative) markers in PyTorch examples ([#&#8203;11112](astral-sh/uv#11112))
-   Fix unnecessary backslashes in brackets ([#&#8203;11059](astral-sh/uv#11059))
-   Suggest setting copy link mode in GitLab integration guide ([#&#8203;11067](astral-sh/uv#11067))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4xNDMuMCIsInVwZGF0ZWRJblZlciI6IjM5LjE1Ni4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

!uv pip install in Jupyter documentation doesn't install into the kernel environment
4 participants