Using Pip to install packages to Anaconda Environment
conda 4.2.13 MacOSX 10.12.1
I am trying to install packages from pip
to a fresh environment (virtual) created using anaconda. In the Anaconda docs it says this is perfectly fine. It is done the same way as for virtualenv.
Activate the environment where you want to put the program, then pip install a program...
I created an empty environment in Ananconda like this:
conda create -n shrink_venv
Activate it:
source activate shrink_venv
I then can see in the terminal that I am working in my env (shrink_venv)
. Problem is coming up, when I try to install a package using pip
:
(shrink_venv): pip install Pillow
Requirement already satisfied (use --upgrade to upgrade): Pillow in /Library/Python/2.7/site-packages
So I can see it thinks the requirement is satisfied from the system-wide package. So it seems the environment is not working correctly, definitely not like it said in the docs. Am I doing something wrong here?
Just a note, I know you can use conda install
for the packages, but I have had an issue with Pillow from anaconda, so I wanted to get it from pip
, and since the docs say that is fine.
Output of which -a pip
:
/usr/local/bin/pip
/Users/my_user/anaconda/bin/pip
** UPDATE **
I see this is pretty common issue. What I have found is that the conda env doesn't play well with the PYTHONPATH. The system seems to always look in the PYTHONPATH locations even when you're using a conda environment. Now, I always run unset PYTHONPATH
when using a conda environment, and it works much better. I'm on a mac.
For others who run into this situation, I found this to be the most straightforward solution:
-
Run
conda create -n venv_name
andconda activate venv_name
, wherevenv_name
is the name of your virtual environment. -
Run
conda install pip
. This will install pip to your venv directory. -
Find your anaconda directory, and find the actual venv folder. It should be somewhere like
/anaconda/envs/venv_name/
. -
Install new packages by doing
/anaconda/envs/venv_name/bin/pip install package_name
.
This should now successfully install packages using that virtual environment's pip!
All you have to do is open Anaconda Prompt and type
pip install package-name
It will automatically install to the anaconda environment without having to use
conda install package-name
Since some of the conda packages may lack support overtime it is required to install using pip and this is one way to do it
If you have pip installed in anaconda you can run the following in jupyter notebook or in your python shell that is linked to anaconda
pip.main(['install', 'package-name'])
Check your version of pip with pip.__version__
. If it is version 10.x.x
or above, then install your python package with this line of code
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'package-name'])
In your jupyter notebook, you can install python packages through pip in a cell this way;
!pip install package-name
or you could use your python version associated with anaconda
!python3.6 -m pip install package-name
I solved this problem the following way:
If you have a non-conda pip as your default pip but conda python is your default python (as below)
>which -a pip
/home/<user>/.local/bin/pip
/home/<user>/.conda/envs/newenv/bin/pip
/usr/bin/pip
>which -a python
/home/<user>/.conda/envs/newenv/bin/python
/usr/bin/python
Then instead of just calling
pip install <package>
, you can use the module flag -m
with python so that it uses the anaconda python for the installation
python -m pip install <package>
This installs the package to the anaconda library directory rather than to the library directory associated with (the non-anaconda) pip
EDIT:
The reason this works is as follows:
the command pip
references a specific pip file/shortcut (which -a pip
tells you which one). Similarly, the command python
references a specific python file (which -a python
tells you which one). For one reason or another these two commands can become unsynchronized, so that your 'default' pip is in a different folder than your default python, and therefore is associated with a different version of python.
In contrast, the python -m pip
construction does not use the shortcut that the pip
command points to. Instead, it asks python to find its version of pip and use that version to install a package.