What is a virtual environment in Python?

A virtual environment is an isolated folder that has its own Python packages for one project. It stops different projects from clashing, for example when one needs version 1 of a library and another needs version 2.

Create and use it with these commands. The first command is the same everywhere.

python -m venv venv

Activate it on macOS or Linux:

source venv/bin/activate

Activate it on Windows:

venv\Scripts\activate

Now install packages with pip, the Python package installer. They go only into this environment.

pip install requests
pip freeze > requirements.txt
pip install -r requirements.txt

pip freeze saves the list of installed packages, and the last command installs them again on another computer. Type deactivate to leave the environment.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.