How to install packages through Pip into a Conda environment
Seemingly ubiquitous in the Python world, the distribution Anaconda comes with over 200 Python packages installed. Anaconda also comes with a package manager called conda.
Besides its utility for installing and managing packages, conda also possesses the ability to create virtual environments which make sharing and reproducing analyses much easier. These virtual environments are created without any Python packages preloaded into them.
Installing Python packages into the virtual environment is often straightforward. You can use the conda install
command to install many packages quickly and easily.
Not all packages are available with conda install
, through, and if you want one that isn’t available then you’ll have to use the alternate package manager pip. It is not at all obvious how conda and pip interact with each other, particularly in the context of virtual environments.
Here is how to install packages using pip inside a conda virtual environment. First thing is to get set up:
- Create your virtual environment with
conda create --name virtual_env_name
, replacingvirtual_env_name
with the name of your virtual environment - Switch to your virtual environment with
source activate virtual_env_name
, again replacingvirtual_env_name
with the name of your virtual environment - Run
conda install pip
, which will install pip to your virtual environment directory
At this point you have two versions of pip
installed: a global version and a version specific to your virtual environment. If you try to run pip install 'package_name'
you’ll use the global version, which will install the package outside your virtual environment.
You need to use the version of pip
inside your virtual environment. To do this you need to find the directory of your virtual environment, which will be somewhere like /anaconda/envs/virtual_env_name/
. You can install packages into your virtual environment with /anaconda/envs/venv_name/bin/pip install package_name
.
That’s all there is to it.