In this tutorial, you will set up a Python environment in Visual Studio Code. The Microsoft Python extension will be introduced, through using the Python extension, you make VS Code into a great lightweight Python IDE.
Step 1: Install the Python Extension
Let’s open VS Code.
First, you click the Extension
button on the left menu to open the Visual Studio Marketplace.
In the Marketplace, search Python
.
You will see tons of Python-related extensions, here we want to install the one developed by Microsoft.
Once you find the Python Extension by Microsoft, click install
button to install. Then VS Code will install for you.
When the installation finished, the extension is automatically enabled on your Visual Studio Code workspace.
Step 2: Select a Python interpreter
Python is an interpreted language, in order to run Python code you must tell VS Code which interpreter to use.
First we open the Command Palette, from top-menu click View > Command
Palette:
In Command Palette, type Python: Select Interpreter
to search, then select the command:
The Python: Select Interpreter
command presents a list of available Python version that VS Code can find antomatically, including virtual environments (we will talk about virtual environment in later tutorials).
The red box below shows all the Python interpreters VS Code find on my laptop.
As you can see, I have plenty of Python interpreters installed, including Python 2, some older Python 3.8.x, and the latest Python 3.10.4 that we have installed in Tutorial 2.
By clicking Python 3.10.4.64-bit
, we select Python 3.10.4 as the Python interpreter will be used by the Python extension.
!!! Note: If you open a workspace (folder) then select Python interpreter, the Python interpreter you selected will be used for that workspace (folder).
What we have done above, selecting without a workspace (folder) open, we sets Python 3.10.4 in Use scope, which means the default interpreter for VS Code in general.
Step 3: Set Execute in File Directory
Next, set the working directory to be the directory of the Python file you want to run, from top menu click File > Preferences -> Settings
:
Search python.terminal.executeInFileDir
and check the box: