Creating and Managing Python Environments
Python is an extremely popular programming language, widely used in diverse domains like web development, artificial intelligence, data science, machine learning, and more. One of the significant advantages of Python is the rich assortment of specialized libraries and frameworks it hosts. But, dealing with multiple Python packages can sometimes be a complex task. The complexity increases when we need different versions of the same package for different projects. This is where Python environments come in handy.

In this comprehensive article, we will discuss how to create and manage Python environments using tools like virtualenv
and conda
. We will begin with the basics, covering why we need Python environments, and will subsequently dive into hands-on examples of creating and managing these environments.
Table of Contents
- Why Python Environments
- Getting Started: The Basic Python Environment
- Introducing Virtual Environments
- Creating a Virtual Environment
- Managing Packages in a Virtual Environment
- Introducing Conda Environments
- Creating a Conda Environment
- Managing Packages in a Conda Environment
- Concluding Thoughts
Why Python Environments?
When writing Python code for different projects, you’ll often need different sets of libraries, sometimes requiring different versions of the same package. Installing all packages globally can lead to version clashes and messy maintenance. In such cases, you need isolated “environments,” each tailored with specific packages required for a specific project.
Python environments enable this feature. You can think of a Python environment as an isolated workspace, with its own Python binary and independently managed packages. Therefore, you can have various environments with distinct package versions, all living happily on the same system.
Python has several tools for managing such environments, including venv
(builtin since Python 3.3), virtualenv
, and conda
. We will focus on virtualenv
and conda
in this guide because of their extensive usage and flexibility.
Getting Started: The Basic Python Environment
The Basic Python Environment is the primary Python installation on your system. After installing Python, you’ll have access to the Python interpreter and pip, the package manager for Python.
python ––version
Python 3.7.5
pip ––version
pip 19.3.1
You can install Python libraries globally using pip. For instance, you can install the widely used ‘requests’ library as follows:
pip install requests
However, installing all your needed libraries like this could lead to problems, paving the way for the need for virtual Python environments.
Introducing Virtual Environments
Virtual Environments, as discussed earlier, are isolated Python workspaces. They are an integral tool in every Python developer’s toolkit because they can help manage dependencies neatly. The easiest way to start using Virtual Environments is through virtualenv
.
pip
manager. Any package installed in this environment will be isolated from the global Python installation.
Before starting with virtualenv
, we need to install it globally. Use the following command for the same:
pip install virtualenv
Creating a Virtual Environment
Creating a new virtual environment is a breeze with virtualenv
. Let’s create one called my_env
:
virtualenv my_env
This command creates a new directory my_env
. This new directory works like a self-contained Python installation. To activate this environment, use the following command:
source my_env/bin/activate
Now you are inside your new Python environment, my_env
. Any pip
installation done now will not affect the global Python installation.
Managing Packages in a Virtual Environment
Once in a virtual environment, you can use pip
to install packages as you would do normally. Suppose, we install the requests
library:
pip install requests
You can check the list of installed packages:
pip freeze
requests==2.25.1
To deactivate the environment, just type:
deactivate
Introducing Conda Environments
Conda is a popular package, dependency, and environment manager, especially favored in the Data Science community.Conda
is part of the Anaconda Distribution, particularly useful when handling Python packages linked with C libraries like NumPy, SciPy, etc.
Creating a Conda Environment
Creating a new Conda environment is very straightforward. To create an environment named conda_env
, use:
conda create -n conda_env
To activate the environment, use:
conda activate conda_env
Managing Packages in a Conda Environment
You can install packages in a conda
environment using both conda install
and pip install
. Let’s install the numpy
package:
conda install numpy
To list the installed packages, use:
conda list
And to deactivate the environment:
conda deactivate
Concluding Thoughts
Python environments, be it virtualenv
or conda
, are an integral part of Python programming, especially when juggling between different projects. It takes a bit of practice getting used to these tools, but once acquainted, they can do wonders in streamlining your Python development process.
Remember, the efficient use of Python environments helps in maintaining clean project states, avoiding package version clash, and reproducing project states easily, leading to a considerable boost in your programming productivity. Happy coding!
Python for Beginners provides a great starting point for those new to Python or programming. You can also find in-depth, comprehensive resources about Python environments in the Python Packaging User Guide.