Jupyter Notebook "!" command not using Virtual Environment

Question:

As we all know we can use !<command> to ensure the cell runs a terminal command.
However if we usepip install lxml it installs lxml in the root python kernel and not the kernel environment that we mention in Jupyter.

With !command in jupyter notebook, any way to use the python virtual env to install the packages?

if os.name=='posix':
    !pip3 install wget
    import wget

This was the code I was trying to run but it installs in python-base and not venv selected in Jupyter notebook

Asked By: Ntropy

||

Answers:

  1. Check your env used by the notebook check venv on.
    Also, your can check your env list ref: In which conda environment is Jupyter executing?
  2. Test import after restarting your python-base.
Answered By: Jimmy Wang

In recent versions of Jupyter you can use the magic %pip command that will target the current kernel rather than the Python installation that is running the notebook.

if os.name=='posix':
    %pip install wget
    import wget
Answered By: Romain