-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Important "gotcha" I just thought of and want to jot down for later:
A fairly common Jupyter notebooks workflow is to install & run the notebook server from a base environment, and add all your various other virtualenv/pyenv/pipenv/venv/conda/etc. environments as kernels to use for running notebooks. A weird side effect of doing this is that although the notebook will use the Python executable and packages installed in the kernel environment, shell commands will run in the notebook server environment:
In[1]: import sys
In[2]: print(sys.executable)
Out[2]: /Users/paxtonfitzpatrick/opt/anaconda3/envs/davos/bin/python
In[3]: !which python
Out[3]: /Users/paxtonfitzpatrick/opt/anaconda3/bin/pythonThis also affects os.environ, the subprocess module, etc., but really all we need is to reference the right pip executable so smuggled packages are installed in the correct environment. I think that will always be f'{sys.exec_prefix}/bin/pip', (same thing as pathlib.Path(sys.executable).parent.joinpath('pip')), but I need to confirm that with a few kinds of environments I don't have locally.