Install And Setup VS Code For Python: Step By Step Guide
In the previous tutorial we have installed python and wrote our python program in a terminal. Well I told you’ll that writing python program in terminal is not efficient and we will use a code editor.
Before proceeding if you have not installed python on your machine then install it by following this Python Installation Tutorial or if installed then continue.
So we will use Visual Studio Code Editor to write & run our python code and in this tutorial we will see how to setup vs code for python.
Why Visual Studio Code
VS Code is a free and open source code editor by Microsoft. It is a quite popular code editor due to its lightweight, versatile, and extensible nature. Below are some of the benefits of using VS Code:
- Works on Windows, macOS, and Linux
- User Friendly Interface
- Extensions for customization
- Integrated Terminal
- Smart code completion and suggestions
- Debugging Support
- Highly Customizable
- Multiple Language Support
- Large Community and Support
- Lightweight and Works Smoothly On Low End Devices
So if you have never used vs code then follow the below steps to install it on your machine or if you have vs code then skip the below steps and go to vs code setup for python.
Installing Visual Studio Code
Step 1: To install VS Code we need to download it’s installer, to download go to VS Code downloads page.
Step 2: Click on blue download button based on the operating system you use. As I use windows I will click on windows download button and it will start download vs code installer.
After the download finishes, click on the vs code installer and it will start the setup.
Step 3: It will ask you to accept their agreement, you can read it if you want and make sure you agree and click on next.
Step 4: Next it will ask you to choose additional tasks, choose everything and click on next.
Step 5: Then it will show ready to install, so click on the install button and It will take around a minute to install vs code on your machine.
After installation it will show you vs code setup completed, click on finish and it will open VS Code.
So we sucessfully installed vs code. Now let’s setup it for python use.
Setting Up VS Code For Python
To setup python in vs code we will install a extension which will help us in writing python code and also configure the python interpreter in vs code.
1. Install Python Extension
Open VS Code, click on the extensions tab or use ctrl+shift+x to open the extensions section, search for python, the first result will display extension named Python by microsoft click on it and install the extension.
Along with python extension it will install some other additional extensions so it will take some time also make sure you are connected to the internet.
2. Configure Python Interpreter
Now to we will need to define the python interpreter in vs code to do that: Open command palette by using the shortcut ctrl+shift+p or in the menu go to view> command palette.
In the command palette search for Python: Select Interpreter, it will show select python interpreter option click on it and now you need to choose the python interpreter it will display the installed python interpreter on your machine select it.
If you have not installed python interpreter then follow this tutorial: Install Python Interpreter On Your Machine.
3. Running Python In VS Code
To write and run python in vs code, click on the explorer tab in the left sidebar, Click on Open Folder so create a folder in your desktop and select it, now it will display your folder name in explorer.
To create a python file inside that folder, move your cursor beside your folder name, it will display a button to create a new file click on it and name the test.py. Make sure the name ends with .py as it defines a python file.
In the python file write print(“Hello World”) and click on the ▶️ button in the top right side. it will open a terminal with output Hello World.
You can also run a python program in vs code by opening a terminal, to open terminal use shortcut ctrl+` or Go to menu: Terminal> New Terminal it will open a terminal in your current folder, In the terminal type python test.py and hit enter.
It will print the same output Hello World which got earlier you can see from below image.
Note: I wrote test.py because my python file name is test.py so you need to write the name of the python file you want to run.
So we successfully installed, setuped and ran python. I hope you were able to follow along and you have setuped python on vs code. From the next chapter we will start looking at python programming syntax.