Are you are confused by the many ways of creating environments in Python?
Do you have to use Conda due to restrictions on the machine you’re working?
This article explains as to why you need Conda. Learn what Conda is and how to use it without wasting your time.
I came across Conda when I was working on a supercomputer. Typically, users do not have permissions to install packages to the global site-packages. That forced me to learn Conda.
Python Virtual Environments (virtualenvs)
The main need is to manage packages without causing conflicts. Some of the use cases where having an isolated virtual environment might help you.
- You might have developed an application. But now it does not work due to a package update or a python version update
- You might want to collaborate with someone. But you want to replicate the same exact environment on both machines
- You might want to work on a Supercomputing facility. But you are a user who does not have permissions to install packages to the global directory.
All of these situation you need individual environments with their own packages or Python versions. There are different tools to manage virtual environments.
Environment Managers
- Virtualenv
- Conda
Let me briefly explain why Virtualenv might not be the best option. In fact, most Supercomputing facilities have now removed the use of it and prefers Conda. Virtualenv installs the packages and versions locally to a project. It’s as simple as going in to your python source directory and declaring that you need a virtualenv by $ virtualenv project
. However, you might end up with a significant number of virtualenvs scattered across your system. Therefore, Conda is preferred.
Why you need Conda
Conda also has the following properties which makes it more suitable to manage virtual environments.
- Packing and distributing software for any language.
- Also acts as a package manager allowing to find and install packages.
- Can’t find you package on Conda? No worries. Pip works well in a Conda virtual environment.
- Better file management (Avoids the issue with virutalenv where it creates environments all around the place).
Installing Conda?
There are tons of resources out there detailing how to install Conda. Please follow this guide to install Conda on your system.
Managing Environments
Next, let’s look at how we could manage virtual environments created by Conda.
1. Adding a new Environment
Adding a new environment with the name myenv
and python version 3.7
conda create -n myenv python=3.7
2. Activating an environment
source activate myenv
3. Installing packages on the environment
Installing packages is very easy. Conda uses channels as repository locations.To install a package (for example intervaltree
— an editable tree structure for python) from a channel ( conda-forge
), which is inside a channel that is not on your channel list, run:
conda install -c conda-forge intervaltree
You could also use,
conda install --channel conda-forge intervaltree
Pip packages are also installable in a Conda environment. For example,
pip install numpy
4. Deactivating an environment
To deactivate the environment you don’t need to specify the environment. Nevertheless, the environment that you’re currently working in will be deactivated.
source deactivate
And that brings us to the end. The objective was to feed you with enough information to get you on your feet! If that was met, please leave a thumbs up.