VSCode Python tutorial¶
VSCode is a popular interactive development environment (IDE) that supports multiple programming languages, including Python. It offers features beyond what a text editor can offer and is relatively easy to use. These features are of critical importance when working with compiled languages, but they are also useful when working with Python.
In this tutorial we will go through how to:
- Install Python
- Install VSCode
- Set up the environment for the project
- Write a simple program
- Use the debugger
- Install and use an external package in our program
Install Python¶
This is covered in more detail in the installation instructions page. A quick summary is provided below.
Windows¶
- Go to the Python download page.
- Choose the
Get standalone installer for Python 3.x.yoption. (Not the "install manager" option). - After downloading, run the file, tick the "Add python.exe to PATH" option, and press
Install Now. - When you see "Setup was successful" you can close this window.
- Now you have "Python interpreter". To verify that, open the Command Prompt application and run the
pythoncommand. It should open the Python interactive shell and show>>>. There you can type and run Python commands. To exit the shell typeexit().
Linux¶
On Linux you are likely to have Python preinstalled. To check this open the terminal and type
python3. If this opens the Python interactive shell and prompts >>> that means that Python is installed and working. You can run Python commands in the shell or quit by using exit().In case Python is not installed or you want to get a newer version, you can run the command below in your terminal.
- For Red Hat, CentOS, or Fedora:
sudo dnf install python3 python3-devel- For Debian or Ubuntu:
sudo apt-get install python3 python3-dev- For Arch Linux:
sudo pacman -S python3
After installation you can check that Python is working with the
python3 command. Use exit() then to exit the shell.Install VSCode¶
- First of all we need to download VSCode from Visual Studio Code website.
- Choose the operating system you are using.
- Download the file and open it.
- You can go through the installation without changing any settings.
- Once in VSCode, go to the extensions, search for the Python extension and install it.
Set up the project environment¶
- First create a new empty folder for the project anywhere you want using the File Explorer application.
- Then open this folder in VSCode by pressing
File->Open Folder. Now this folder is your "workspace". - If asked "Do you trust the authors of the files in this folder?" choose
Yes, I trust the authors
Next we will create a "virtual environment". This helps keep the project separate from other projects. Any packages installed inside the virtual environment won’t be visible outside of it. This prevents version conflicts between packages in different projects and is considered the best practice among python developers.
- Press
Ctrl + Shift + P. This will open the Command Palette. TypePython: Create Environmentthere and click on the corresponding option. - Then choose
Venvas environment type. - Choose the Python interpreter that you installed earlier from the appeared list.
- If you are not sure which one it is or if the list is empty you can choose
Enter interpreter path. Then open the Command Prompt app and typewhere pythonon Windows orwhich python3on Linux. That shows the location where you have installed Python. Copy the path and paste it into VSCode (the path with "Python" and not "Microsoft" in it). - Virtual environment is ready now. You should see
.venvfolder appearing under your workspace folder in the Explorer on the left. It has its own copy of Python Interpreter which we will use to run Python code in this project. It is located at.venv\Scripts\pythonon Windows and at.venv\bin\python3on Linux.
Write a simple program¶
- In the Explorer on the left use the button to create a new file and name it. Use
.pyextension. - Write the following code in the file:
hello_message = "Hello world!"print(hello_message)- Save the file using
Ctrl + S. - Run the file with the button on the top right. It will open the terminal and run the command for you. You should see
Hello world!in the output. - If for some reason the Run button doesn't work, you can type the command yourself. To do so open the terminal using
Ctrl + `and type.venv/Scripts/python main.py(Linux: bin instead of Scripts folder). Here we point to the interpreter we want to use and then to the file we want to run. - There can also be an error in red in the output on the first run, but then it goes away.
Use debugger¶
The debugger is included in the Python extension for VSCode, which you have already installed. It gives you more control when your code runs and helps you find issues more easily.
- First place a breakpoint on the second line. It shows as a red circle and stops the debugger when it gets to this line.
- Then open the "Run and Debug" panel on the left and press
Run and Debug. - In the appeared list choose
Python Debuggerand thenPython File.
Now the debugger will start running our code and will pause at the breakpoint. At this point we can see:
- The variables in our program and their values on the top left.
- We can add custom expression to watch. For example, "hello_message[0]" in the picture below gets the first character of the string.
- The buttons to control the debugger on top include:
- Continue - runs the program until the next breakpoint (or until program end).
- Step over and Step into - go to the next line of code. (If it is a function call Step into will jump to the function and Step over won't).
- Step out - steps out of the function.
- Restart - restarts the debugger.
- Stop - stops the debugger.
As our program is very small, the debugger is not very useful here. Once you will write longer programs and face bugs in them remember about the debugger.
Install and use a package¶
It often happens that we need to use the external libraries, which expand the functionality available to us. For example, if we want the text we print in the terminal to look a different color we can use the
We will now change our code so that it looks as follows:
colorama package to achieve that.We will now change our code so that it looks as follows:
import colorama
hello_message = "Hello World!"
print(colorama.Fore.GREEN + hello_message)
print(colorama.Style.RESET_ALL)
However, when we run our code (you may now use the debug mode or just run) we get an error saying "No module named 'colorama'".
Colorama isn’t included with the default Python installation, and we haven’t installed it ourselves yet. To add it, we’ll use pip, the tool for installing Python packages.
Since we want to install it inside our virtual environment (project specific, not globally), we need to use the pip that belongs to this environment, located at
.venv/Scripts/pip (you can also see it in the Explorer on the left, next to the Python interpreter we use, if you open corresponding folder). For Linux instead of Scripts folder there is bin folder.We then run pip with the install command followed by the package name.
In our case, that gives us:
In our case, that gives us:
.venv/Scripts/pip install colorama for Windows and.venv/bin/pip install colorama for LinuxAfter installation, you can run the program and see our greeting printed in green now!