This article will explain and demonstrate virtual environments in Python using virtualenv and in Anaconda.
What is a ‘Virtual Environment’?
In Python, a virtual environment is an isolated directory that contains all of the dependencies of a Python project.
By default, when you install a package using the pip package manager, it is installed globally – that version of the package is available to all Python scripts running on your system.
This can cause problems. It clutters up your global Python installation, and all projects are stuck using the same version, which may not be appropriate if you start a new project that relies on a newer version of a package that is incompatible with the version used in your previous projects.
It also makes portability and collaboration difficult. You may not have the same dependencies installed on another machine you work on, or those you collaborate with on code may not wish to install the package globally for fear of conflicting with their own configurations.
Virtual environments solve this – when activated, all packages are installed in the project directory (locally rather than globally). Everything is isolated, removing the potential for conflicts.
Virtual environments in Python with virtualenv
virtualenv provides virtual environments for the standard release of Python. Once installed globally, it can be used to create and activate virtual environments for you to work in:
To create a virtual environment and install NumPy, run the following commands:
# Install virtualenv globally (the default Python environment) pip install virtualenv # Create a new virtualenv environment, which will act as the root of your project virtualenv -p python3 MyProject # Change directory to the project cd MyProject # Activate the virtual environment bin/activate # Install a package to test the virtual environment pip install numpy
Above, the NumPy package is installed to test the virtual environment.
If you want to return to your global (default) Python environment, run:
bin/deactivate
Recording your Project’s Requirements/Dependencies
Once you are working in a virtual environment, you can output all of the installed packages to a text file so that you can easily reproduce it by running:
python -m pip freeze > dependencies.txt
To install the packages listed in the file (for example on a different computer), run:
python -m pip install -r dependencies.txt
Virtual Environments in Anaconda
Anaconda, a popular data science platform that includes Python, has it’s own virtual environments implementation as well, which works much the same way:
# Create and activate a new environment for your project conda create -n MyProject conda activate MyProject # Install a package to test the virtual environment conda install numpy