Anaconda 和 Brew 的最佳实践
问题描述
我刚买了一台装有 OSX Sierra 的新 Macbook,所以想确保我的开发环境设置正确.
我希望遵循此处提到的最佳实践":https://github.com/nicolashery/mac-dev-setup
我需要 Python 2.x 来工作(urllib、Pandas、Numpy、Scikit-learn),而 Python 3.x 需要我正在学习的一些在线课程(Pandas、Numpy、Django).我已经分别使用 brew install python
和 brew install python3
安装了 Python 2 和 3.
但是,在这个链接上,没有提到 Anaconda,只有 IPython.鉴于我已经通过 Homebrew 安装了 Python 2 和 3,是否建议使用 anaconda,或者我应该坚持使用上面 Github 链接中提到的标准 IPython?读完这篇文章后我很困惑:OS X - 在 anaconda 之间决定和自制 Python 环境
如果 Brew 和 Anaconda 确实可以一起工作,我可以采取哪些具体步骤来确保两个版本之间没有冲突?
解决方案brew
和 conda
往往不能很好地结合在一起,但我想我有一个设置到目前为止对我来说效果很好.它的灵感来自这篇文章.p>
您可以将以下代码添加到您的.zshrc
:
# 在运行 brew 之前停用 conda.# 如果 conda 在完成时处于活动状态,则重新激活它.酿造(){本地 conda_env=$CONDA_DEFAULT_ENV"而[$CONDA_SHLVL"-gt 0];做康达停用完毕命令冲泡$@本地 brew_status=$?[ -n ${conda_env:+x}"] &&conda 激活$conda_env"返回$brew_status"}
您想在运行 brew
之前停用 conda
以便 brew
在您的PATH
每当它尝试安装某些东西时.事实上,如果您在运行 brew 之前没有停用 conda
,brew doctor
会报错,正如我在上面链接到的帖子中提到的那样.(另请参阅这个问题.)
我应该提到的一件事是 conda
环境堆栈",但是我上面编写的 brew()
函数不会跟踪您的堆栈环境.(有关跟踪此功能的版本,请参见下文.)例如,如果您执行 conda activate newenv
而 conda
环境 oldenv
处于活动状态,然后 conda deactivate
会将您返回到 oldenv
.但是,如果您在激活 oldenv
和 newenv
后使用我上面编写的函数运行 brew
,则运行 conda deactivate
不会将您返回到 oldenv
,但会完全停用您的 conda 环境.
这个函数在运行 brew
时也可能会产生一些不必要的开销,因为我相信你只需要在运行 brew install时关闭你的
conda
环境代码>.也就是说,如果你是那种足够关心开销的人,那么这个答案可能不会告诉你任何你不知道的事情.
最后一点,brew cask install anaconda
在我看来并不是一个好主意,因为 conda
被设计为安装在 $HOME
,但 brew cask
会希望将其安装在 /usr/local
中,这样可能会导致不可预知的行为.
这是 brew
函数的一个版本,它使您的 conda 环境保持原样:
brew() {本地 -a conda_envs而[$CONDA_SHLVL"-gt 0];做conda_envs=("$CONDA_DEFAULT_ENV"$conda_envs)康达停用完毕命令冲泡$@本地 brew_status=$?$conda_envs 中的环境;做conda 激活$env";完毕未设置环境返回$brew_status"}
我已经在 Zsh 中对此进行了测试.我认为它不会在 Bash 中工作.如果你想在 Bash 中使用它,你需要将 for 循环声明更改为类似于 for env in ${conda_envs[@]}
的内容.但是,我没有对此进行测试,因此请在使用前测试它是否能满足您的需求.
I have just got a new Macbook with OSX Sierra, so want to ensure my development environment is setup properly.
I am looking to follow the 'best practices' mentioned here: https://github.com/nicolashery/mac-dev-setup
I need Python 2.x for work (urllib, Pandas, Numpy, Scikit-learn), and Python 3.x for some online classes (Pandas, Numpy, Django) I am taking.
I have installed Python 2 and 3, using brew install python
and brew install python3
respectively.
However, on this link, there is no mention of Anaconda, just IPython. Given that I already have Python 2 and 3 installed via Homebrew, is it even advisable to use anaconda, or should I stick to standard IPython as mentioned on the Github link above? I am confused after reading this post: OS X - Deciding between anaconda and homebrew Python environments
If Brew and Anaconda can indeed work together, what specific steps can I take to ensure that there are no conflicts between the two versions?
解决方案brew
and conda
tend not to play nicely together, but I think I have a set up that has worked quite well for me so far. It was inspired by this post.
You can add the following code to your .zshrc
:
# Deactivates conda before running brew.
# Re-activates conda if it was active upon completion.
brew() {
local conda_env="$CONDA_DEFAULT_ENV"
while [ "$CONDA_SHLVL" -gt 0 ]; do
conda deactivate
done
command brew $@
local brew_status=$?
[ -n "${conda_env:+x}" ] && conda activate "$conda_env"
return "$brew_status"
}
You want to deactivate conda
before running brew
so that brew
doesn't find conda
packages in your PATH
whenever it tries to install something. In fact, brew doctor
will complain if you have not deactivated conda
before running brew, as mentioned in the post I link to above. (See also this question.)
One thing I should mention is that conda
environments "stack", but the brew()
function I've written above does not keep track of your stack of environments. (See below for a version of this function that keeps track of this.) For example, if you do conda activate newenv
while a conda
environment oldenv
is active, then conda deactivate
will return you to oldenv
. However, if you run brew
using the function I've written above after activating oldenv
and then newenv
, running conda deactivate
will not return you to oldenv
but will deactivate your conda environments entirely.
This function also probably creates some unnecessary overhead when running brew
, as I believe you only really need to deactivate your conda
environment when running brew install
. That said, if you're the kind of person to care about that overhead enough, this answer probably doesn't tell you anything you didn't already know.
As a final note, brew cask install anaconda
does not strike me as a good idea, since conda
was designed to be installed in $HOME
, but brew cask
will want to install it in /usr/local
, so that could lead to unpredictable behaviour.
Edit: Here's is a version of the brew
function which leaves your conda environments as it found it:
brew() {
local -a conda_envs
while [ "$CONDA_SHLVL" -gt 0 ]; do
conda_envs=("$CONDA_DEFAULT_ENV" $conda_envs)
conda deactivate
done
command brew $@
local brew_status=$?
for env in $conda_envs; do
conda activate "$env"
done
unset env
return "$brew_status"
}
I've tested this in Zsh. I don't think it will work in Bash. If you want to use it in Bash, you will need to change the for loop declaration to say something like for env in ${conda_envs[@]}
. I haven't tested this, however, so please test that it does what you need before use.
相关文章