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

Include sys.path of ephemeral env in PYTHONPATH #7699

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

tfsingh
Copy link
Contributor

@tfsingh tfsingh commented Sep 26, 2024

This PR ensures that the sys.path of an ephemeral environment is included in the PYTHONPATH of spawned processes (useful in the case of executing external commands). This addresses issue #7647.

I tested this with the repro in the issue, i.e.:

uv init
uv add jupyter
uv run --with pydantic jupyter lab

An automated test is trickier — Jupyter doesn't have an interface for executing commands non-interactively (closest thing is jupyter console, which spawns a repl). Its runtime (ipython) does accept a -c flag, but the issue doesn't manifest when using ipython — as such, a test wouldn't be effective. Open to thoughts on more rigorous test plans, I might be missing something!

@tfsingh tfsingh changed the title Include sys.path of ephemeral env in PYTHONPATH of external command Include sys.path of ephemeral env in PYTHONPATH Sep 26, 2024
@tfsingh tfsingh marked this pull request as ready for review September 26, 2024 08:45
@zanieb
Copy link
Member

zanieb commented Sep 26, 2024

You might just be able to do something like python -c "import sys; print(sys.path)"?

@charliermarsh
Copy link
Member

A few issues with this that come to mind though I'm not certain on either:

  1. I think entries in PYTHONPATH have slightly different precedence / prioritization than they would if you run uv run here without a notebook. You can probably see the difference if you print out sys.path within the notebook and with uv run --with pydantic python -c "import sys; print(sys.path)".
  2. I think that using PYTHONPATH won't follow .pth files though @carljm would know -- in which case, we wouldn't be able to resolve any --with-editable dependencies added to the uv run command.

@bluss
Copy link
Contributor

bluss commented Sep 26, 2024

I'm concerned that setting PYTHONPATH could have an impact on uv run inside uv run and so on (which becomes more common as uv gets more useful to run everything!). When setting PYTHONPATH there is risk that python environments bleed into each other and packages are imported between environments that should be separate.

This was issue #5459 in the past.


You have two environments, Base and Layer.

The root cause of the problem is that Base/bin/jupyter is a python script that hard-codes that it runs using the Base/bin/python interpreter.

Could a solution be to execute Layer/bin/python Base/bin/jupyter? That would activate the correct environment.

@charliermarsh
Copy link
Member

I've tried uv run --with pydantic python -m jupyter lab and it doesn't work despite using the layered Python :/

@bluss
Copy link
Contributor

bluss commented Sep 26, 2024

I just discovered that. 😢 jupyter lab must be executing the jupyter-lab script stub?

@charliermarsh
Copy link
Member

Yeah there's something going on that I don't understand yet.

@bluss
Copy link
Contributor

bluss commented Sep 26, 2024

And if I "fix" the jupyter-lab script stub to just use usr/bin/env python, a different error is produced. This is not going to play very well with environment layering, because jupyterlab is searching for its $prefix/share/jupyter/lab

JupyterLab Error
JupyterLab application assets not found in "/home/user/.cache/uv/archive-v0/5CUs8Rso39JXZO4nfVoDF/share/jupyter/lab"
Please run `jupyter lab build` or use a different app directory

@tfsingh
Copy link
Contributor Author

tfsingh commented Sep 26, 2024

Could a solution be to execute Layer/bin/python Base/bin/jupyter? That would activate the correct environment.

This was the first thing I tried as well, using the ephemeral env's interpreter to execute Base/bin/jupyter — I'll need to dig deeper here to understand this.

A few issues with this that come to mind though I'm not certain on either:

Both of these points make sense as well (verified the first, looking into the second now)

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.

4 participants