問題描述
我剛買了一臺裝有 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.
這篇關于Anaconda 和 Brew 的最佳實踐的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!