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 different versions of package dependencies and Python. After 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 effective 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.
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 standard library, and it’s the officially 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.
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:
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:
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 absolute path to its Python interpreter when executing a command. However, most commonly, you’ll want to activate the virtual environment after creating it to save yourself the effort of repeatedly having to type long absolute 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
After creating and activating your virtual environment, you can now install any external dependencies that you need for your project:
Read the full article at https://realpython.com/python-virtual-environments-a-primer/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]