What is Conda? What Can It Do?
Anaconda is a Python dependency managemnt tool. It gives you three things:
- Dependency Management. It lets you install and remove packages.
- Environment isolation. It’s a environment management tool like NVM or RVM that lets you create isolated environments with their own dependencies, versions, and Python versions.
- Repository of curated libraries. Conda has a few thousand hand-verified libraries that you can rely on.
As you can see, it combines several very useful tools into one helpful package.
Installing Conda
Installation is easy. A simple double click install on On Windows and OSX, or a shell script on Linux. Instructions here:
Conda Environments
Typing:
conda info
at the command line will list some details about your current conda environment. You should notice something like this:
active environment : base
This means we are in the base environment, a general environment for all our projects. Ideally we want to create a specific environment for our project. We can do this with conda create:
conda create -n my-project
This creates the environemt, but doesn’t start using it. To activate the env, type:
conda activate my-project
If you have something like Oh My ZSH running you should see the command prompt change to let you know where you are. Nice.
Installing packages
We can now install a package from the Anaconda repo with Conda install, thus:
conda install jellyfish
Having sone this, we can import it from any python script, and expect it to work:
import jellyfish
Magic.
Note that Conda environments do not stack. You can’t install a library in Base and expect it to be installed everywhere. Conda environments are all isolated one from the other.
Listing packages
To see all the packages you have installed type:
conda list
Listing environments
To see a list all the environments you have created, type:
conda env list
Deleting en environment
To destroy an environment, type:
conda deactivate
Note that this is quite a common thing to do. Destroying and recreating an environment is not a big deal in with Anaconda, especially if you have a requirements.txt.
Usage with Pip
Conda is fully compatible with Pip. Libraries installed with Pip are local to the package you are currently working inside, and, provided you’re using Conda’s version of Pip, are not global.
Pip using PyPy, which has a LOT more packages, but some of them might be broken or dangerous. Conda has a select few packages, but all of them work and will not infect your computer with herpes.
pip install nose
…makes nose
available in this environment and no other.
Setting an environment specific version of Python
To use a specific version of Python, you have to create the environment with the python flag:
conda create --name test-env-2 python=3.8
You can’t change the version of Python in a running environment, you have to make a new one, so you may want to conda deactivate
first. This won’t be painful if you have a requirements.txt file.
Loading packages from a requirements.txt
If you want to share an environment, you can put all your requirements in a file. By convention this is called requirements.txt, but you can call it anything. It’s a simple, newline separated list of all your requirements.
To install from a requirements.txt, first create your environment with conda create, then install from the file, like so:
conda install --file requirements.txt
Saving packages to requirements.txt
You can save your current environment to a requirements.txt file, just by piping conda list -e
into the file:
conda list -e > requirements.txt
You can then recreate this environment easily with:
conda install --file requirements.txt
To sum up
So there we have it. A dependency tool, an environment manager, and a list of curated packages, all in one tidy bundle. Conda is pretty great, and you should probably know about it if you’re using Python.