Accessing pip packages from Anaconda

Viewed 428

I recently updated my macOS version and had to reinstall Anaconda. I had always tried to install packages via pip so that they would be accessible whether using Anaconda (Spyder, specifically) or executing files in the terminal (without Anaconda, e.g. python myfile.py). However, after the reinstall, it looks like Anaconda is unable to access packages that are installed through pip but not through conda.

I'm aware that I can activate a given environment in Anaconda and then use pip to install packages for that environment, but I'm looking for global access to packages so that I don't have to install packages multiple times.

Is there a workaround to so that pip packages can be accessed universally?

EDIT: When I installed Python 3 on macOS, I followed the guide here: https://opensource.com/article/19/5/python-3-default-mac#what-to-do.

2 Answers

It appears that you are using different Python installations in Spyder and terminal. Therefore packages installed with pip in the terminal will not be visible in Spyder and vice-versa. If you want to have access to the same packages in both, the best solution is to activate Anaconda Python (or its specific conda environment) in the terminal.

You can activate Anaconda automatically in the terminal by running conda init (it will include the activation commands to your ~/.bashrc file).

I faced the same problem before. I just share my solution. If you think this is not helpfull. You can leave a comment, then I can delete the answer.

Write in front, my solution give up the anaconda pip and default system pip. If you just want use anaconda. Then just read my solution as knowledge supplement or just skip my answer.

In my understanding, you need a way to control your python environment. Then you can build the same environment on any computer.

First, use anaconda manage the python version.

You can use

conda create --name python3.6 python=3.6

create specific python.

Then use

conda activate python3.6

to activate the specifaic version python3.6. You can add this to your .bashrc to auto activate the specfic version.

Second, install virtual environment management tool. I usually use pipenv. Install it by

pip install pipenv

Third, create virtual environment for your project.

cd your project folder
pipenv shell
pipenv install --skip-lock your package

Then you will can see it generate a pipfile under your project. If you keep it by upload your project to github or other way. Then you can build the virtual envriment any machine like this:

  1. install conda.
  2. activate specific version python and install pipenv.
  3. git clone your project and cd into it.
  4. pipenv shell
  5. pipenv install --skip-lock.

Then you will get a same environment as the pipfile.

Related