The easiest way to run Python in a VS Code Virtual Environment is with the Git Bash terminal included with Git for Windows.
Note that as of 2022-06-01, Git Bash and Command Prompt terminals prefix commands with &
instead of source
. This is a VS Code issue that is being fixed but has not been released. Issue does not affect PowerShell terminal.
The workaround is given here: (microsoft/vscode-python/issues/16175)[microsoft/vscode-python#16175]. In short, modify your settings.json
.
- Open VS Code.
- In the bottom-left corner, click the gear icon > Settings.
- In the upper-right corner, click the Open settings.json icon (file).
- Add the following lines and save:
"terminal.integrated.profiles.windows": {
"Git Bash": {
"path": "C:\\Program Files\\Git\\bin\\bash.exe",
"source": "Git Bash",
"args": [],
"icon": "terminal-bash"
},
},
"terminal.integrated.defaultProfile.windows": "Git Bash",
Install these extensions.
- Microsoft Python
- Microsoft Pylance
VS Code venv can be a little finicky to start. These are the steps I follow. Once configured, the environment will automatically activate any time you open the project directory in VS Code.
- Open project folder in VS Code.
- Use
Ctrl+Shift+~
to open the terminal. - In the upper-right corner, use the New Terminal icon (+) to open a bash shell if needed.
# Create / open a .py file
# Create venv - this takes a minute
# Adds a venv folder to project
python -m venv venv
# Activate - there is no visual confirmation
venv/Scripts/activate.bat
# Command Palette > Python: Select Interpreter > Enter path
# Browse to and select `project\venv\Scripts\python.exe`
# Kill / start new terminal. May need to restart VS Code.
# Prompt now shows (venv). Do a sanity check.
which pip
# Update
python -m pip install --upgrade pip setuptools wheel
# Use requirements.txt
# https://pip.pypa.io/en/stable/reference/requirements-file-format/
python -m pip install -r requirements.txt --upgrade
Reason: Modules are installing globally instead of in the venv. Check:
which pip3
points tovenv/Scripts/pip3
.venv/Lib
for the installed packages.
Reason: No empty __init__.py
in root + subfolders.
- Add
__init__.py
.