Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to install pip packages inside my Conda environment?

Tags:

python

pip

conda

As I understand it, if I use pip install ___, that package will go to my global version of python. If I change directory to the within my Conda environment then that package will be isolated within the environment. Is this correct?

I have searched to try and find where to put the pip packages (within my Conda environment). It used to be that you would install the pip packages in /Anaconda3/envs/venv_name/bin/. It appears the bin folder is now located within the Library folder, like this: /Anaconda3/envs/venv_name/Library/bin. Is the bin folder still the recommended place to put the packages installed by pip?

In other words should I be placing the pip installed packages here: /Anaconda3/envs/venv_name/Library/bin ?

like image 948
Nick T Avatar asked Feb 15 '19 16:02

Nick T


1 Answers

No Specification Needed

Fortunately, one need not manually specify where to install the packages. Instead, if one uses the pip associated with the environment, packages will install to the site-packages directory of environment's python by default.

Example

 > conda activate venv_name
 
 # check that you are using the right pip
 > which pip
 /Anaconda3/envs/venv_name/bin/pip  # should be something like this

 > pip install <package name>

This will install packages into /Anaconda3/envs/venv_name/lib/python3.7/site-packages/, or whatever Python version you have installed for the environment.

⛔️ Important Note: There are some flags for pip install that change this behavior, most notably the --user flag. Conda users are strongly discouraged from using this flag because it installs packages at a user-level, leading to packages being visible to other environments with matching Python versions (major+minor).

Caution: Mixing PyPI and Conda Packages

Be aware that (as @WilliamDIrons pointed out), it is usually preferable to use conda install -n venv_name <package name> instead of pip. The common practice is to only use pip in a Conda environment when the package is not available through a Conda repository. It is strongly recommended to read and follow the best practices found in the "Using pip in an environment" documentation.

like image 54
merv Avatar answered Nov 07 '22 21:11

merv