Selecting a Python Virtual Environment in VS Code for Jupyter Notebooks

Follow these steps to select a Python virtual environment located in the .venv/ directory of your current workspace for use with Jupyter notebooks in Visual Studio Code (VS Code).

Open Command Palette

  • Press Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS) to open the Command Palette.

Select Interpreter

  • Type Python: Select Interpreter and hit Enter.

Choose the .venv Environment

  • From the list, select the interpreter that corresponds to your .venv directory.

Activate the Virtual Environment

  • The selected interpreter will now be used for running Jupyter notebooks.

Use the Status Bar

  • Click on the interpreter version displayed on the Status Bar at the bottom to switch interpreters.

Automatic Activation

  • VS Code may automatically suggest the .venv environment when the folder is present in your workspace.

Configure Workspace Settings for Persistent Use

  • Modify the settings.json in the .vscode folder with the following entry:
json
{
    "python.pythonPath": "${workspaceFolder}/.venv/bin/python"
}

Replace the path with the correct one if your operating system or directory structure is different.

Note: Ensure that the Python extension is installed and that your .venv is properly set up. If the virtual environment was created while VS Code was open, you may need to reload VS Code.