How To Create A Python Virtual Environment
A step-by-step guide to working with venv.
If you are like me and have dozens of different Python projects, managing and maintaining the correct packages, libraries, and versions can become quite a problem. The dependencies of one project may differ from another, or certain library versions may not be suitable for a specific codebase. Using virtual environments is a great way to keep your projects organized and separate from each other. They are used for a variety of reasons, from isolating your project’s dependencies, to keeping your package versions consistent, to creating separate environments for staging and production.
In this guide, we will be creating a virtual environment for a simple Python project. We will be using the venv
module, which is included in Python 3.6 and above. If you are using an earlier version of Python, the virtualenv
module can be used.
Let’s get started!
Installing venv
venv
should be installed by default with Python 3.6 or above, but if it is not installed, you can do so with the command:
pip install venv