Frequently Asked Questions#
Can I use virtual environments with VS Code?#
This is particularly tricky configuration since it depends on the way you have setup VS Code.
VS Code works in two ways:
Workspaces
A special folder with custom VS Code configurationsBasically if your
EXPLORER
shows a top folder name according to a folder, you will be working in a workspace.One cannot change the interpreter in VS Code workspaces (as one would generally do.
In this case you can only get virtual environments to work by following this:
Open the VS Code terminal (press Ctrl+Shift+P, or Command+Shift+P on MacOS) and search for
python env
, select the menuPython: Create Environment
Now two additional options open up, choose
Venv
Wait until it is done.
Execute a single line of code, e.g.
exit()
+ Shift+Enter to force the virtual environment being activated in the terminal associated with the workspace (you should see that the left of the terminal shows.venv
). Then it will execute theexit()
code and return to the terminal.Alternatively you can press the button to run the currently opened script.
Warning
The terminal will not be in the virtual environment if VS Code is freshly opened and you have not executed any Python code.
Click this box to get information about workspaces.
File editing
Single file scriptsIf not in a workspace (see above), you can freely select the interpreter.
If you have a Python file open (extension
.py
), you will see aPython
block at the bottom right. To the right of this small box, you can press a button which lets you choose the interpreter.This should be a full path to the python interpreter (a Python executable). There will generally be a list of Python executables present to choose from, if your wanted interpreter is not present, you’ll have to navigate manually to the wanted interpreter.
Click this box to get additional details regarding changing the interpreter.