0% found this document useful (0 votes)
129 views3 pages

Python Virtual Environments A Primer - Real Python

This document provides an overview of Python virtual environments, including how to create, activate, install packages into, and deactivate a virtual environment. It explains the benefits of using virtual environments such as avoiding system pollution, sidestepping dependency conflicts, and minimizing reproducibility issues. The document also discusses how virtual environments work under the hood and options for customizing and managing multiple virtual environments.

Uploaded by

Luboš Řehounek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
129 views3 pages

Python Virtual Environments A Primer - Real Python

This document provides an overview of Python virtual environments, including how to create, activate, install packages into, and deactivate a virtual environment. It explains the benefits of using virtual environments such as avoiding system pollution, sidestepping dependency conflicts, and minimizing reproducibility issues. The document also discusses how virtual environments work under the hood and options for customizing and managing multiple virtual environments.

Uploaded by

Luboš Řehounek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Python Virtual Environments: A Primer – Real Python https://realpython.

com/python-virtual-environments-a-primer/

Python Virtual Environments: A Primer


by Martin Breuss  Apr 13, 2022  105 Comments  devops intermediate tools

Mark as Completed   Tweet  Share  Email

Table of Contents
• How Can You Work With a Python Virtual Environment?
◦ Create It
◦ Activate It
◦ Install Packages Into It
◦ Deactivate It
• Why Do You Need Virtual Environments?
◦ Avoid System Pollution
◦ Sidestep Dependency Conflicts
◦ Minimize Reproducibility Issues
◦ Dodge Installation Privilege Lockouts
• What Is a Python Virtual Environment?
◦ A Folder Structure
◦ An Isolated Python Installation
• How Does a Virtual Environment Work?
◦ It Copies Structure and Files
◦ It Adapts the Prefix-Finding Process
◦ It Links Back to Your Standard Library
◦ It Modifies Your PYTHONPATH
◦ It Changes Your Shell PATH Variable on Activation
◦ It Runs From Anywhere With Absolute Paths
• How Can You Customize a Virtual Environment?
◦ Change the Command Prompt
◦ Overwrite Existing Environments Help
◦ Create Multiple Virtual Environments at Once
◦ Update the Core Dependencies

1 z 33 02.03.2023 12:18
Python Virtual Environments: A Primer – Real Python https://realpython.com/python-virtual-environments-a-primer/

◦ Avoid Installing pip


◦ Include the System Site-Packages
◦ Copy or Link Your Executables
◦ Upgrade Your Python to Match the System Python
• What Other Popular Options Exist, Aside From venv?
◦ The Virtualenv Project
◦ The Conda Package and Environment Manager
• How Can You Manage Your Virtual Environments?
◦ Decide Where to Create Your Environment Folders
◦ Treat Them as Disposables
◦ Pin Your Dependencies
◦ Avoid Virtual Environments in Production
◦ Use Third-Party Tools
• Conclusion

 Remove ads

 Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written

tutorial to deepen your understanding: Working With Python Virtual Environments

In this tutorial, youʼll learn how to work with Pythonʼs venv module to create and manage separate virtual environments for your
Python projects. Each environment can use di�erent versions of package dependencies and Python. A�er youʼve learned to work
with virtual environments, youʼll know how to help other programmers reproduce your development setup, and youʼll make sure
that your projects never cause dependency conflicts for one another.

By the end of this tutorial, youʼll know how to:

• Create and activate a Python virtual environment


• Explain why you want to isolate external dependencies
• Visualize what Python does when you create a virtual environment
• Customize your virtual environments using optional arguments to venv
• Deactivate and remove virtual environments
• Choose additional tools for managing your Python versions and virtual environments

Virtual environments are a common and e�ective technique used in Python development. Gaining a better understanding of
how they work, why you need them, and what you can do with them will help you master your Python programming workflow.

Free Bonus: Click here to get access to a free 5-day class that shows you how to avoid common dependency
management issues with tools like Pip, PyPI, Virtualenv, and requirements files.

Throughout the tutorial, you can select code examples for either Windows, Ubuntu Linux, or macOS. Pick your platform at the
top right of the relevant code blocks to get the commands that you need, and feel free to switch between your options if you
want to learn how to work with Python virtual environments on other operating systems.

 Take the Quiz: Test your knowledge with our interactive “Python Virtual Environments: A Primer” quiz. Upon
completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

How Can You Work With a Python Virtual Environment?


If you just need to get a Python virtual environment up and running to continue working on your favorite project, then this
section is the right place for you.

The instructions in this tutorial use Pythonʼs venv module to create virtual environments. This module is part of Pythonʼs

2 z 33 02.03.2023 12:18
Python Virtual Environments: A Primer – Real Python https://realpython.com/python-virtual-environments-a-primer/

standard library, and itʼs the o�icially recommended way to create virtual environments since Python 3.5.

Note: There are other great third-party tools for creating virtual environments, such as conda and virtualenv, that you can
learn more about later in this tutorial. Any of these tools can help you set up a Python virtual environment.

For basic usage, venv is an excellent choice because it already comes packaged with your Python installation. With that in mind,
youʼre ready to create your first virtual environment in this tutorial.

 Remove ads

Create It
Any time youʼre working on a Python project that uses external dependencies that youʼre installing with pip, itʼs best to first
create a virtual environment:

 Windows  Linux  macOS

Windows PowerShell

PS> python -m venv venv

If youʼre using Python on Windows and you havenʼt configured the PATH and PATHEXT variables, then you might need to provide
the full path to your Python executable:

Windows PowerShell

PS> C:\Users\Name\AppData\Local\Programs\Python\Python310\python -m venv venv

The system path shown above assumes that you installed Python 3.10 using the Windows installer provided by the Python
downloads page. The path to the Python executable on your system might be di�erent. Working with PowerShell, you can find
the path using the where.exe python command.

Activate It
Great! Now your project has its own virtual environment. Generally, before you start using it, youʼll first activate the environment
by executing a script that comes with the installation:

 Windows   Linux + macOS

Windows PowerShell

PS> venv\Scripts\activate
(venv) PS>

Before you run this command, make sure that youʼre in the folder that contains the virtual environment you just created.

Note: You can also work with your virtual environment without activating it. To do this, youʼll provide the full path to its
Python interpreter when executing a command. However, most commonly, youʼll want to activate the virtual environment
a�er creating it to save yourself the e�ort of repeatedly having to type long paths.

Once you can see the name of your virtual environment—in this case (venv)—in your command prompt, then you know that your
virtual environment is active. Youʼre all set and ready to install your external packages!

Install Packages Into It


A�er creating and activating your virtual environment, you can now install any external dependencies that you need for your
project:

 Windows   Linux + macOS

3 z 33 02.03.2023 12:18

You might also like