
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.

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

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

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.

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

Also, the program is working just fine.

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

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.

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

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.
NOTE:
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. https://pypi.org/, where the module details are specified. Copy that and paste it in your cmd.

Also Read:
- Top 25 Pattern Programs in C++
- Currency Converter in C++
- SQLite | CRUD Operations in Python
- Number Guessing Game in C++
- Image background remover in Python
- C++ Project Structure
- Python | Check if a string is a palindrome or not without Recursion
- Python | Check if a number is an Armstrong Number or not using Recursion
- Python | Check if a number is an Armstrong Number or not without using Recursion
- Python | Shuffle a list using recursion
- Python | Shuffle a list without recursion
- Python | Implementing switch case using functions
- Python function to check whether a number is perfect or not
- Python | Find LCM using function
- Python | Find HCF using function
- Python | Convert the binary number to decimal without using library function
- Python | Create a basic operations calculator(+, -, /, and x), create a separate function for each operation
- Python | Detecting the number of local variables declared in a function
- Python | Making a chain of function decorators (bold italic underline etc)
- Python | Access function inside a function
- Event Management System Project in Python
- ATM machine program in C++
- Python | Create a function with a pass statement
- Python | Function to calculate the square root of a number
- Python | A function that calculates the power of a number
- Python | A function that accepts 2 integers and adds them and returns their sum
- Python | Function that takes a list of integers and returns the last integer
- Python | Return multiple values from a function
- Python function that takes a list and returns a new list with unique elements of the first list
- Python | Generate a random number in the range of 1 to 10 using the function