In this tutorial, we are going to discuss how to set up VS Code for Python programming.
What is VS Code?
It also has a very large ecosystem of extensions for supporting several other frameworks and programming languages like C, C++, Python, etc. These extensions are the most important feature of the VS Code which redefines the experience of code editing takes it to the next level.
Let’s discuss how we can configure our Visual Studio Code for Python programming.
1. Download VS Code
As the very first step, we have to download the latest version of Visual Studio Code from its official website code.visualstudio.com.
Steps to install VS Code for Python
After the download of the VS Code installer file completes, follow the steps given below to install the software on your local (Windows) machine.
- Double click on the installer file.
- Select the destination folder where you want to install VS Code.
- You can optionally create a start menu folder where the setup will create the program’s shortcuts.
- After these you can select some additional taskes which you want the VS Code setup to perform like:
- Create a desktop icon
- Add “Open with Code” action to Windows Explorer (file context menu)
- Add “Open with Code” action to Windows Explorer (directory or folder context menu)
- Register VS Code as an editor for the supported file types
- Add to path which requires shell restart
- Click Install to begin the installation process. Once done, click Finish.
2. Install the Python Interpreter for VS Code
Now, we have to download the latest version of Python Interpreter from its official website www.python.org.
Steps to install Python Interpreter
After you download the Python installer file, follow the steps given below to install the interpreter on your local (Windows) machine.
- Double click the installer file.
- Check the option “Add Python to PATH”.
- Click on one of the two options shown on the screen
- Install Now option will install the Python Interpreter with the default settings (recommended for beginners)
- Customize Installation option will allow us to enable or disable the features
- Finally, click Close which appears after the installation process completes.
We can also verify if the Python Interpreter has been successfully installed by running the following commands on the terminal:
- For Windows, run the following command on the Command Prompt or the PowerShell.
C:\Users\Guest> py -3 --version
- For Linux or macOS machine run the following command on the terminal.
ubuntu:~$ python3 --version
The outputs in the case of both the commands will show the version of the Python installed on the system only if the Python is successfully installed on the system.
3. Install the Python extension for VS Code
After installing the VS Code Software as well as the Python Interpreter, we have to install the Python extension for VS Code. Python extension is a Visual Studio Code extension developed by Microsoft that has numerous supporting features such as IntelliSense, Code navigation, Code formating, linting, debugging, etc. for all the supported versions of Python language (>=3.6).
Steps to install the Python extension
To install the Python extension for VS Code follow the steps given below:
- Open the VS Code Extension Marketplace using the shortcut (Ctrl+Shift+X).
- Search for the Python extension by typing “Python” in the Extension Marketplace search bar.
- Select the first option with name Python from the search result which is star marked.
- Then click on the Install button.
NOTE: When we install the Python extension for VS Code, the Pylance and Jupyter extensions automatically get installed to provide us the best possible experience while working with the Python (
.py) files and Jupyter (
.ipynb) notebooks. The Pylance and Jupyter extensions are optional dependencies even if we uninstall these extensions the main Python extension will remain fully functional.
Useful features of Python extension
- IntelliSense (Pylance)
- Code Formating
- Jupyter Notebook (Jupyter)
4. Different modes in VS Code to work with Python
In Visual Studio Code, we have three different modes to work with Python language. Let’s discuss them one by one.
Python Script File
In this mode, we can either create a new Python file or open an existing Python file with
.py extension in VS Code. But we can only run the Python script file as a whole on the VS Code integrated terminal.
Python Interactive Mode
In this mode, we create a normal Python script file then we have to activate the Python Interactive Mode in it by typing [
# %% ]. This will create one code cell for us where can we execute the Python instructions distinctly either by clicking Run Cell or by using the shortcut (Shift+Enter). Once we run a code cell an interactive window will open on the side of the Python script file. We can also debug our Python instructions within the individual code cell by pressing the Debug Cell.
Interactive Python Notebook or Jupyter Notebook
In this mode, we can either create a new Python Jupyter Notebook or open an existing Python Jupyter Notebook with
.ipynb extension in VS Code. Here we can only run the Python instructions within an already presented code cell. We cannot run all the Python instructions in the Jupyter Notebook as a whole on the VS Code integrated terminal. To run the individual code cell either press the Run Cell button above each code cell or by using the shortcut (Shift+Enter).
In this tutorial, we have learned what is VS Code for Python, how to download and install it on the local windows machine. We have also learned about various VS Code extensions like Python, Pylance, and Jupyter which have rich support for the Python language. We too learn to work with different Python modes in VS Code like Python script file, Python interactive mode, and Python Jupyter Notebook. Hope you have set up your Visual Studio Code software and ready to code in Python language on it.