How do I use Conda in on Homebrew Python system?

2020-05-26 09:37发布

问题:

I'm using Homebrew as my package general manager, and am using its Python and pip for software development, along with virtualenvs. For various reasons, I'd like to continue with this structure, but I need some software that is (apparently) easier to install using Conda.

Can I continue to use Homebrew+pip+virtualev and add Conda into the mix, ideally inside a virtualenv so that it doesn't affect my system as a whole? If so, how do I set up and use Conda in this way?


(Python: 2.7.11 (Homebrew); pip: 8.1.1; setuptools: 20.6.7; OS X: 10.11.4 (x86_64))

回答1:

You can install Anaconda. Try brew cask install anaconda. Follow the on-screen instructions you may want to add export PATH=/usr/local/anaconda3/bin:"$PATH" to your ~/.bash_profile or ~/.zsh file.

Using anaconda you can create virtual environments for python2 and python3. You can set up environments and then use commands like source activate py27 assuming py27 is an environment you created in python2.7 in anaconda. It even has GUI and CLI versions.

Every time I open my terminal, my .bash_profile and .zshrc get sourced. If you append the source line above, it will load with the version you need everytime. Every anaconda environment has its own pip as well. With Anaconda in the mix, you'd not really need virtualenv anymore but you can keep it if you want.

If you want to run the Anaconda Navigator GUI you can run it: open /usr/local/anaconda3/Anaconda-Navigator.app. You can use it to manage/create the environments and pip packages, etc.



回答2:

I may be wrong but it sounds like the op is trying to figure out how to make a virtual environment for anaconda without overriding all their existing python stuff. At least that is what I was trying to do which led me here. I managed to find a rather inelegant solution that you can use if you really must have anaconda in its own env:

As devssh says, you can do a brew cask install anaconda but dont add the directory to your path, or it will override your python 2.7 and cause much sadness. Instead create a virtual environment like such (Im using virtual environment wrapper):

mkvirtualenv -p /usr/local/anaconda3/bin/python anaconda_env

now move all the stuff from anaconda bin into your virtualenv bin:

cp /usr/local/anaconda3/bin/* /Users/<you>/.virtualenvs/anaconda_env/bin/

This last is necessary because the anaconda dependencies were not installed with the environments pip so it doesnt know where to look for them.