Search code examples
pythonshellterminalpipjupyter-notebook

Jupyter Notebook "!" command not using Virtual Environment


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


Solution

  • 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