Search code examples
pythonvisual-studio-codejupyter-notebookjupyterpython-venv

pip install Python packages in Jupyter Notebook in a virtual environment


I would like to create a Jupyter Notebook in a virtual environment in VS Code. I want to do different kinds of projects. For that, I have tried to create a virtual environment inside VS Code, where I can download the required packages for the project i.e. TensorFlow, PyTorch... And I want to do those projects in a Jupyter Notebook because I also would like to present them.

  1. First, I created a folder in desktop. [Step 1]: enter image description here
  2. I dragged the folder on VS Code window. [Step 2]: enter image description here
  3. Created a Jupyter Notebook file called "Project1.ipynb" [Step 3]: enter image description here
  4. Then Ctrl+Shift+P, and chose "> python:Create Environment" -> Type "venv" and the later Python version that I have as interpreter [Step 4]: enter image description here
  5. At last, I moved the Jupyter Notebook file to ".venv" virtuasl environment. enter image description here
  • So my question is how can I pip install specific python packages to that virtual environment and use them just in that particular virtual environment and not globally ?

Solution

  • One way is to install package in venv environment in the terminal.

    Firstly, active your venv environment by command like Path\to\venv\active

    enter image description here

    Then you can use command pip install packageName to install the python package you want.

    Finally, don't forget to choose the python kernel in jupyter.

    enter image description here

    Another way is to run codes ! pip install packageName in cells after choosing the python kernel.

    enter image description here