Installing Python and dev environment with Visual Studio Code

This post is lesson 2 of 54 in the subject Python Programming Language

1. Introduction to Python programming language

Python is a high-level programming language, used for various purposes. Python is used in web development, machine learning applications, and more. It was introduced by programmer Guido van Rossum in 1991 and developed by the Python Software Foundation.

Python is designed to be easy to read, learn, and remember. Python’s structure allows for writing programs with shorter code. Python is a very clear language, with a clear structure, making it very suitable for beginners to learn programming.

Python is one of the most popular programming languages today.

Most popular programming languages
Most popular programming languages (01/2019 – source statista.com)

2. Installing Python on Windows 10

There are two main branches of Python versions: Python 2 and Python 3. They are quite different. Python 2 was released in 2000 and developed up to version 2.7.18 before being discontinued. Python 3 is still under development and as of now (29/01/2022), there is already a version of Python 3.10.2.

In the Python programming language series, we will be using Python version 3.10.2 on Windows 10. You can download it from the official Python website with the link below.

Double-click on the Python installation file to start the installation. The installation interface has the following options:

(1) Install with default path and configurations

(2) Edit path and configurations

(3) It is recommended to configure the Python environment in Windows

After successful installation, you will see the Python installation folder.

You can go to the Start on Windows 10 to view the installed Python programs.

Python programs install in Start
Python programs install in Start on Windows 10

The IDLE (Python 3.10 64-bit) program is a simple integrated development environment for Python. IDLE is packaged with the Python installation program from version Python 1.5.2b1.

The Python 3.10 (64-bit) program is the Python interpreter. Any source code written in Python needs to be interpreted by the Python interpreter in order to run.

To check the version of Python you have installed, you can use the python command in Windows 10 Command Prompt.

3. Installing Visual Studio Code

Visual Studio Code is a source code editor developed by Microsoft. It is very lightweight, supports many programming languages, and has exciting features such as debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and embedded Git.

In the Python programming language series, we will be using Visual Studio Code to edit Python source code. You can download Visual Studio Code for Windows 10 from the official Visual Studio Code website with the link below.

Double-click on the Visual Studio Code installation file to start the installation.

Click Next and then select the installation path for Visual Studio Code.

After successful installation, you will see the Visual Studio Code installation folder.

Below is the interface when starting “Get Started” for Visual Studio Code.

4. Setting up the Python programming environment with Visual Studio Code

To program Python with Visual Studio Code, we need to install the Python extension for Visual Studio Code. The Python extension released by Microsoft automatically recognizes the Python interpreter and supports code completion, error suggestions, debugging, code formatting, and more.

To install the Python extension in Visual Studio Code, select Extensions or press Ctrl+Shift+X and type python to install the Python extension.

After installing the Python extension, in Visual Studio Code, we navigate to the Explorer section or press Ctrl+Shift+E to open a folder where we will create Python source code files. In the example below, a python-examples folder is created in the C partition and contains files with the extension .py. These files are Python source code files.

(1) Area for Python source code files

(2) Area for editing Python source code

(3) The interpreter is automatically recognized when opening a .py file

(4) Click to execute Python source code

(5) Terminal area to display Python program results

4.1. What is a virtual environment in Python?

A Python application often has libraries with different versions. In many cases, we program for multiple applications at the same time. It will be very inconvenient and cause unnecessary errors if these applications use different library versions but collide with each other.

For example, application A uses library abc version 1.7. Application B also uses library abc but version 2.1. Both these libraries have the method1() function, but they will process differently due to different versions. When running applications A and B, it is easy to cause errors or make the program run incorrectly. These cases occur very often in practice when you have to maintain old projects and develop new ones.

4.2. Installing a virtual environment for Python programs

To better manage libraries, Python allows creating separate virtual environments that contain separate libraries for each application. To create a virtual environment for Python in Visual Studio Code, we navigate to the Terminal area, type the command python -m venv .venv. In which .venv is the name of the virtual environment created. The name of the virtual environment is chosen by you.

In the above example, we create a virtual environment .venv in the folder C:\python-examples of a Python application. Then, we use the command .venv\Scripts\activate to activate the virtual environment. .venv is the virtual environment’s name and this folder contains the virtual environment. While in the virtual environment, we can install libraries for this virtual environment only with the command pip install <library_name>. Or to exit the virtual environment, use the command deactivate.

In this article, you have learned how to install Python and Visual Studio Code, and how to set up a Python programming environment with Visual Studio Code. If you encounter any errors, please comment below, and we will fix them together!

5/5 - (2 votes)
Previous and next lesson in subject<< Introduction to Python Programming LanguageHow is a Python program executed? >>

Leave a Reply

Your email address will not be published. Required fields are marked *