Run Python Code, Install Libraries, Create a Virtual Environment | VS Code

Run Python Code, Install Libraries, Create a Virtual Environment | VS Code

Visual Studio Code is one of the most efficient code compilers/interpreters. It is very promising because of the vast and widely available go-to extensions that help programmers.

This article is an elaborative detail about how we can run Python code, install Python libraries, and create a virtual environment in Visual Studio Code.

Write and Run Python Code in VS Code

To run Python in VS Code, just create a new .py file and write any Python Code you want.

run python in vs code
Image/GIF source->

Install Libraries in VS Code

If you are writing your .py script and you’re stuck somewhere seeing this red line in VS Code.

error in code due to the library not being installed

And when you are trying to run the program, you see the terminal with the following error,

error message in terminal

This is because you are importing the module which is not installed.

There are 2 ways we can install the libraries in VS Code:

1. Using the pip command

Very straightforward, just install what packages you need.

Open Terminal in VS Code. The shortcut is to press Ctrl+Shift+`

And install the specific module you want to through pip. Most of the time the command is

pip install module-name

And hit ENTER.

Sit back and relax, and let the pip do its job of installation.

Install Libraries using pip

And now you can see that the red line is disappeared.

python code in vs code

Also, the program is working just fine.

output of python program

This means every time you want to install a specific library, just open the terminal, type in the command terminal, and hit Enter. The libraries are getting installed here globally, which means inside your system, you can run the code by importing it from any directory.

2. Using the virtual environment

This is one of the best and most practiced ways. Here the libraries you are installing will not be installed actually in your main system.

What is the virtual environment?

According to docs, a virtual environment is a Python environment such that the Python interpreter, libraries, and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.

In a very sober way, you actually are installing the libraries within those folders and within the environment. If you are trying to run the same code from any other .py file from any other directory, you will see that the library isn’t installed.

It is actually installed only for that environment, only for that directory where you are creating the virtual environment.

How to create a Virtual Environment?

Open the terminal in VS Code. The shortcut is to type in Ctrl+Shift+`.

Copy the folder path i.e. the path where you want to create a virtual environment. Note that, this should be the same folder where your .py file is located.

Type in the terminal.

python -m venv folder-path\venv
creating virtual environment folder

Don’t forget to add the \venv after the folder your specified.

Now open the terminal again by either clicking on New Terminal or pressing Ctrl+Shift+`

And now you can see you are in a virtual environment.

virtual environment created

And now, here, type in the commands to install the library.

installing library in virtual environment

And hurray, the library is installed. You are good to go.

This is how we can install the library in VS code in two ways.


Not every time, in Python for the installation, the module name will be the same as the name we used to import. eg. Opencv – you have to type ‘pip install OpenCV-python’. In this case, just type in install ‘module’ in the search box of your web browser and go to the first official website of Python i.e., where the module details are specified. Copy that and paste it in your cmd.


Also Read:


Author: Ayush Purawr