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:
- Installing Python 3.7.3 (64 bit)
- Configuring Visual Studio Code IDE for Python development
- Code, Debug and Execute Python programs from Visual Studio Code
- Executing Python programs from the command line
- 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.
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.
We can give a quick glance inside the Control Panel, to verify the time stamp of the installation.
Also, we can verify that Python’s path as being added to the environment variables.
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.
Configuring Visual Studio Code IDE for Python development
Open Visual Studio Code. Click on View -> Extensions menu option.
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.
Selecting the Python Interpreter
Inside Visual Studio Code, Click View -> Command Palette….
It will display the command palette. Inside the command palette type “Select Interpreter” and select “Python: Select Interpreter”.
It will display the list of available Python interpreter. Please select Python 3.7.3 64 bit.
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.
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
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”.
Type “print("\Hello Python 3.7.3!")” inside the first.py. Please refer to the image below.
Inside Visual Studio Code, click on “Debug -> Start Debugging”, it will display the debugging options. Please select the “Python File” option.
It will add the launch.json file. Please refer to the image below.
Update the code inside the first.py file.
-
-
-
-
-
-
- import datetime
-
-
- todays_date = datetime.datetime.now()
- today = todays_date.strftime('%c')
-
-
- name = input("Enter Your Name: ")
- employee_id = int(input("Enter Employee Id: "))
- salary = float(input("Enter Salary: "))
- is_manager = bool(int(input("Enter Manager Status {1 OR 0}: ")))
-
-
- print("====================================================")
- print(f"Employee Details | Date: {today} |")
- print("====================================================")
- print(f"\tName: {name}")
- print(f"\tId: {employee_id}")
- print(f"\tSalary: {salary}")
- print(f"\tIs Manager: {is_manager}")
- 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.
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.
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.
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.
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!