Setting Up Python 3.7.3 Visual Studio Code On Windows 10

Introduction

 
As developers, we all need a good IDE (Integrated Development Environment) to Code, Debug and execute programs. Visual Studio Code is one of the good IDE's which can be used for Python development. This article will discuss the following:
  1. Installing Python 3.7.3 (64 bit)
  2. Configuring Visual Studio Code IDE for Python development
  3. Code, Debug and Execute Python programs from Visual Studio Code
  4. Executing Python programs from the command line
  5. Executing Python programs using pyLauncher

Installing Python 3.7.3 (64 bit)

 
Visit here to download Python 3.7.3 executable (python-3.7.3-amd64.exe) to the local machine. By default, python-3.7.3-amd64.exe will get downloaded into the download folder.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
Right-click on python-3.7.3-amd64.exe and select “Run as administrator”. It will display a dialog box.
 
Please ensure that we have selected the checkbox with the label “Add Python 3.7 to PATH”.
 
Once the checkbox is selected, please click on the “Install Now” link.
 
Please refer to the image below. It will display a dialog with the “Setup Progress” title and will take a couple of minutes to complete the installation.
 
Once the installation is done, it will display a dialog with the “Setup was successful” title.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
We can give a quick glance inside the Control Panel, to verify the time stamp of the installation.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
Also, we can verify that Python’s path as being added to the environment variables.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
Please open a command prompt and type “python”, it will take us into REPL (Read Evaluate Print Loop).
 
We can see the python version is being displayed. Please refer to the image below.
 
Please type “print("\nHello Python 3.7.3")” at the prompt, it will print “Hello Python 3.7.3”. Type exit() to come output of Python REPL.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 

Configuring Visual Studio Code IDE for Python development

 
Open Visual Studio Code. Click on View -> Extensions menu option.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
Inside the “Search Extensions” text field, please type “Python” to display the list of extensions for Python. Please select the Python extension from Microsoft and Install. This will enable the Python extension for Visual Studio Code.
 
Also, install VSCode-Icons from VSCode Icons Team.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 

Selecting the Python Interpreter

 
Inside Visual Studio Code, Click View -> Command Palette….
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
It will display the command palette. Inside the command palette type “Select Interpreter” and select “Python: Select Interpreter”.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
It will display the list of available Python interpreter. Please select Python 3.7.3 64 bit.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
Once the Python Interpreter is selected, we can see the visual indicator in the Visual Studio Code’s status bar. Please refer to the image below.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 

Install PyLint and AutoPep in Visual Studio Code

 
PyLint
 
Pylint is a Python static code analysis tool that looks for programming errors, helps to enforce a coding standard, sniffs for code smells and offers simple refactoring suggestions.
 
Autopep8
 
autopep8 automatically formats Python code to conform to the PEP8 style guide.
 
Inside Visual Studio Code, click on View -> Terminal menu option to display the terminal. Please type the below mentioned two commands to install PyLint and AutoPep packages.
  • python.exe -m pip install -U pylint --user
  • python.exe -m pip install -U autopep8 --user
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
Now we are all set for coding in Python 3.7.3.
 

Code, Debug and Execute Python programs from Visual Studio Code

 
Create a folder called PythonPrograms inside C:\ (in our case, it is C:\LordKrishna\GitHub\speaker_series_python\Beginner\PythonPrograms) and open it in Visual Studio Code (File -> Open Folder ...). Mouse over the PythonPrograms folder, it will display few icons. Please click on the “New File” icon and name it as “first.py”.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
Type “print("\Hello Python 3.7.3!")” inside the first.py. Please refer to the image below.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
Inside Visual Studio Code, click on “Debug -> Start Debugging”, it will display the debugging options. Please select the “Python File” option.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
It will add the launch.json file. Please refer to the image below.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
Update the code inside the first.py file.
  1. #!/usr/bin/env python3  
  2. """ 
  3.     Program to demonstrate Basics of Input/Output 
  4. """  
  5.   
  6. # Importing Modules  
  7. import datetime  
  8.   
  9. # Calculating the date  
  10. todays_date = datetime.datetime.now()  
  11. today = todays_date.strftime('%c')  
  12.   
  13. # Accepting Inputs from User  
  14. name = input("Enter Your Name: ")  
  15. employee_id = int(input("Enter Employee Id: "))  
  16. salary = float(input("Enter Salary: "))  
  17. is_manager = bool(int(input("Enter Manager Status {1 OR 0}: ")))  
  18.   
  19. # Displaying the output  
  20. print("====================================================")  
  21. print(f"Employee Details | Date: {today} |")  
  22. print("====================================================")  
  23. print(f"\tName: {name}")  
  24. print(f"\tId: {employee_id}")  
  25. print(f"\tSalary: {salary}")  
  26. print(f"\tIs Manager: {is_manager}")  
  27. print("----------------------------------------------------")  
Once the launch.json file is added, please place the cursor on line 20 and click on Debug -> Toggle Breakpoint OR F9 OR click on the left side of line 20. It should create a breakpoint with a small red circle. Please refer to the image below. Please click on Debug icon on the toolbar OR press F5 key OR Debug -> Start Debugging option to debug/execute the program.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
The program execution stops at line 20 where we placed the breakpoint. On the left-hand side, we are seeing the “Locals” window which shows the values of the variables within the scope. We can also use the “Watch” window for additional debugging. We can view the “Call Stack” window to find out the hierarchy of the calls.
 

Executing Python programs from the command line

 
We need IDE till we fully bake the program. Once the program is fully developed, we can start executing it with the command line. Open the command prompt. Navigate to the folder where we created the first.py. Type python first.py to execute the Python program from the command line.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 

Executing Python programs using pyLauncher

 
Python Launcher - Python launcher for the Windows platform. A Python launcher is a single executable that uses several heuristics to locate a Python executable and launch it with a specified command line.
 
From the command prompt, we can directly type the filename.py (in our case first.py) without prefixing the python. The program still gets executed.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 
Alternatively, we can right mouse click on the first.py and click open option, it will launch command with executing the .py file using the python.
 
Setting Up Python 3.7.3 Visual Studio Code On Windows 10
 

Summary

 
In this article, we have installed the Python 3.7.3 64 bit version and learned how to configure Visual Studio Code to work with Python development. Also, we learned a couple of ways to execute the .py program from the command line and also with PyLauncher. Happy Coding!


Recommended Free Ebook
Similar Articles