Skip to content

Jupyter notebooks: make sure shell commands use proper executables for Python environment #10

@paxtonfitzpatrick

Description

@paxtonfitzpatrick

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/python

This 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.

Metadata

Metadata

Labels

JupyterIssues specifically affecting the Jupyter Notebook implementation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions