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) orCmd+Shift+P(macOS) to open the Command Palette.
Select Interpreter
- Type
Python: Select Interpreterand hit Enter.
Choose the .venv Environment
- From the list, select the interpreter that corresponds to your
.venvdirectory.
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
.venvenvironment when the folder is present in your workspace.
Configure Workspace Settings for Persistent Use
- Modify the
settings.jsonin the.vscodefolder 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.
