{}const=>[]async()letfn</>var
DevelopmentPython

Python virtual environments: why you need them and how to use them

Learn how virtual environments help avoid library version conflicts and isolate the dependencies of each project. A step-by-step guide to creating, activating, and managing virtual environments for beginner Python developers.

К

Kodik

Author

4 min read

What is a virtual environment?

A virtual environment is an isolated environment for a Python project that has its own set of installed packages. It's like a separate room for each project, where only the libraries it needs live.

Why do you need insulation?

Without virtual environments, all packages are installed globally in the system. This leads to problems:

Version conflicts. Different projects may require different versions of the same library. Without isolation, you will have to choose one version for all.

System contamination. Over time, a lot of unused packages accumulate, which are difficult to track and remove.

Reproducibility issues. When a colleague or you try to run a project on another computer, it is difficult to understand which packages are needed.

🔥 100,000+ students already with us

Tired of reading theory?
Time to code!

Kodik — an app where you learn to code through practice. AI mentor, interactive lessons, real projects.

🤖 AI 24/7
🎓 Certificates
💰 Free
🚀 Start learning
Joined today


Creating the first virtual environment

Python comes with a built-in venv module that allows you to create virtual environments without installing additional tools.

Open the terminal in your project folder and run the command:

python -m venv venv

Here, the first word venv is the name of the module, and the second is the name of the folder for the environment. You can use any name, but venv or .venv have become the de facto standard.

After executing the command, the venv folder will appear with the following structure:

venv/
├── bin/          # activation scripts (on Windows — Scripts/)
├── include/      # header files
├── lib/          # installed packages
└── pyvenv.cfg    # environment configuration

Activation and deactivation

Creating an environment is not enough — it needs to be activated. After activation, all pip and python commands will work inside this environment.

On Linux and macOS:

source venv/bin/activate

On Windows:

venv\Scripts\activate

After activation, (venv) will appear at the beginning of the terminal line — this means that the environment is active.

To exit the virtual environment, simply type:

deactivate

Installing packages

Now that the environment is activated, you can install the necessary libraries:

pip install requests
pip install django==4.1

All packages will be installed in the venv/lib folder, not in the system. You can check the list of installed packages with the command:

pip list

Requirements.txt file

The requirements.txt file is used so that other developers (or you on another computer) can easily recreate the environment. This is a simple text file with a list of all project dependencies.

You can create it with the command:

pip freeze > requirements.txt

The contents of the file will be approximately as follows:

requests==2.28.1
Django==4.1.0
certifi==2022.9.24

Now anyone can install all the necessary packages with one command:

pip install -r requirements.txt

Alternatives to venv

Although venv is included in the standard Python library, there are other tools for working with virtual environments:

virtualenv — an older tool that supports additional features and runs faster on some systems.

pipenv — combines dependency and virtual environment management, uses Pipfile files instead of requirements.txt.

poetry — a modern dependency manager with advanced project management and package publishing capabilities.

conda — popular in Data Science, as it can install not only Python packages, but also system libraries.

For beginner developers, venv remains the best choice due to its simplicity and the lack of need to install anything extra.

Solving typical problems

Environment does not activate on Windows? You may need to allow scripts to run. Run PowerShell as administrator and execute:

Set-ExecutionPolicy RemoteSigned

The python command doesn't work? On some systems, you need to use python3 instead of python.

Forgot whether the environment is activated? Check with the command which python (Linux/macOS) or where python (Windows). The path should point to the folder of your virtual environment.

Conclusion

Virtual environments are the basic tool of any Python developer. They solve many problems and make working on projects more convenient and predictable. Take some time to master this technique, and it will pay off many times over in your future projects.

Want to learn Python in depth?
We invite you to Code — here you will find structured courses for developers of any level. And we also have a cool Telegram channel with a friendly community where you can ask questions, share experiences and get support from like-minded people!

🎯Stop procrastinating

Liked the article?
Time to practice!

In Kodik, you don't just read — you write code immediately. Theory + practice = real skills.

Instant practice
🧠AI explains code
🏆Certificate

No registration • No card