python - Using Pip to install packages to Anaconda Environment

ID : 10168

viewed : 53

Tags : pythonpipanacondaenvironmentpython

Top 5 Answer for python - Using Pip to install packages to Anaconda Environment

vote vote

96

For others who run into this situation, I found this to be the most straightforward solution:

  1. Run conda create -n venv_name and conda activate venv_name, where venv_name is the name of your virtual environment.

  2. Run conda install pip. This will install pip to your venv directory.

  3. Find your anaconda directory, and find the actual venv folder. It should be somewhere like /anaconda/envs/venv_name/.

  4. 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!

vote vote

90

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 
vote vote

80

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

vote vote

66

This is what worked for me (Refer to image linked)

  1. Open Anaconda
  2. Select Environments in the left hand pane below home
  3. Just to the right of where you selected and below the "search environments" bar, you should see base(root). Click on it
  4. A triangle pointing right should appear, click on it an select "open terminal"
  5. Use the regular pip install command here. There is no need to point to an environment/ path

For future reference, you can find the folder your packages are downloading to if you happen to have a requirement already satisfied. You can see it if you scroll up in the terminal. It should read something like: requirement already satisfied and then the path

[pip install anaconda]

vote vote

52

If you didn't add pip when creating conda environment

conda create -n env_name pip 

and also didn't install pip inside the environment

source activate env_name conda install pip 

then the only pip you got is the system pip, which will install packages globally.

Bus as you can see in this issue, even if you did either of the procedure mentioned above, the behavior of pip inside conda environment is still kind of undefined.

To ensure using the pip installed inside conda environment without having to type the lengthy /home/username/anaconda/envs/env_name/bin/pip, I wrote a shell function:

# Using pip to install packages inside conda environments. cpip() {     ERROR_MSG="Not in a conda environment."     ERROR_MSG="$ERROR_MSG\nUse \`source activate ENV\`"     ERROR_MSG="$ERROR_MSG to enter a conda environment."      [ -z "$CONDA_DEFAULT_ENV" ] && echo "$ERROR_MSG" && return 1      ERROR_MSG='Pip not installed in current conda environment.'     ERROR_MSG="$ERROR_MSG\nUse \`conda install pip\`"     ERROR_MSG="$ERROR_MSG to install pip in current conda environment."      [ -e "$CONDA_PREFIX/bin/pip" ] || (echo "$ERROR_MSG" && return 2)      PIP="$CONDA_PREFIX/bin/pip"     "$PIP" "$@" } 

Hope this is helpful to you.

Top 3 video Explaining python - Using Pip to install packages to Anaconda Environment

Related QUESTION?