尽管安装了 Anaconda,Mac 仍使用默认 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22773432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:38:59  来源:igfitidea点击:

Mac using default Python despite Anaconda install

pythonmacos.bash-profileanacondaconda

提问by intl

I am running Mac 10.9 Mavericks and have installed Anaconda. However, despite that, when I access python via terminal, I still get the default Apple version:

我正在运行 Mac 10.9 Mavericks 并安装了 Anaconda。然而,尽管如此,当我通过终端访问 python 时,我仍然得到默认的 Apple 版本:

Python 2.7.5 (default, Sep  2 2013, 05:24:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin

My .bash_profile is this:

我的 .bash_profile 是这样的:

export PATH="$HOME/anaconda/bin:$PATH"

MONGO_PATH=/usr/local/mongodb/bin
SQL_PATH=/usr/local/mysql

export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH"

Is there anything I can do to use the Anaconda version of Python? At a loss at the moment.

我可以做些什么来使用 Anaconda 版本的 Python?一时不知所措。

Thank you

谢谢

采纳答案by Heap

The first matching executable is the one that is run. From what I can gather you are concatenating your PATH variable in such a way that:

第一个匹配的可执行文件是运行的那个。据我所知,您正在以这种方式连接 PATH 变量:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

comes before:

出现在:

$HOME/anaconda/bin

So make surethat the anaconda directory is the firstone, meaning that it will have precedence:

因此,请确保anaconda 目录是一个目录,这意味着它将具有优先级

export PATH="$HOME/anaconda/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH"

回答by Hyman Fisher

If you use LiClipse or Eclipse as your IDE, the Preferences menu will allow this management process to go much easier. But I understand the joy of the command line.

如果您使用 LiClipse 或 Eclipse 作为您的 IDE,首选项菜单将使这个管理过程变得更加容易。但我理解命令行的乐趣。

It is super easy to make Anaconda, or rather Anaconda's Python version the default interpreter in LiClipse, as well as call the site-packages from Anaconda. I just set it up today.

使 Anaconda 或 Anaconda 的 Python 版本成为 LiClipse 中的默认解释器,以及从 Anaconda 调用站点包非常容易。我今天刚设置好。

Regards,

问候,

JF

杰夫

回答by sushmit

if you are using zsh you can edit in your zshrc file in your root folder to include

如果您使用的是 zsh,则可以在根文件夹中的 zshrc 文件中进行编辑以包含

export PATH="$HOME/anaconda/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH"

回答by Zhang Buzz

If you are using fish, you can find Anaconda backup your old .bash_profile as .bash_profile-anaconda.bak, and it added 2 lines at the bottom of .bash_profile which looks like this:

如果您使用的是鱼,您可以找到 Anaconda 将您的旧 .bash_profile 备份为 .bash_profile-anaconda.bak,并在 .bash_profile 底部添加了 2 行,如下所示:

# added by Anaconda2 4.1.1 installer
export PATH="/Users/username/anaconda/bin:$PATH"

However fish does not read it, so you have to add it in fish config file manually, which is in ~/.config/fish/config.fish:

但是 fish 不会读取它,因此您必须手动将其添加到 fish 配置文件中,该文件位于 ~/.config/fish/config.fish 中:

set -x PATH /Users/username/anaconda/bin $PATH

回答by Reihan_amn

Make sure you are using the full path: - don't use "~" instead of the root:

确保您使用的是完整路径: - 不要使用“~”而不是根:

(wrong)

(错误的)

export PATH="~/anaconda/bin:$PATH"

(Correct)

(正确的)

export PATH="$HOME/anaconda/bin:$PATH"

This change worked for me!

这个改变对我有用!

回答by russellthehippo

Updatefor all people seeing this with Python 3: above solutions will not work with Python 3.

更新所有使用 Python 3 看到此问题的人:上述解决方案不适用于 Python 3。

Anaconda's Python 3 is now at ~/anaconda3/bin. So instead do:

Anaconda 的 Python 3 现在位于~/anaconda3/bin. 所以改为:

export PATH="$HOME/anaconda3/bin:$PATH"

or

或者

export PATH="$HOME/anaconda3/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH"

回答by Andrei

For newer versions of mac OS anaconda is now installed under ~/opt/anaconda. If you have my zsh follow these steps:

对于较新版本的 mac OS,anaconda 现在安装在 ~/opt/anaconda 下。如果您有我的 zsh,请按照以下步骤操作:

  1. vim .bash_profile => here you should see at the bottom these comments
  1. vim .bash_profile => 在这里你应该在底部看到这些评论
    added by Anaconda3 2019.10 installer
  1. Add everything in between these comments to the bottom of your .zshrc file and restart your terminal.
  1. 将这些注释之间的所有内容添加到 .zshrc 文件的底部并重新启动终端。

回答by ethanc9931

Using OSX 10.15, Andrei's answer worked for me (after googling exasperatedly for an hour).

使用 OSX 10.15,安德烈的回答对我有用(在恼怒地谷歌搜索一个小时之后)。

Here was the block of text to save the step from above:

这是保存上面步骤的文本块:

# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/opt/anaconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
    \eval "$__conda_setup"
else
    if [ -f "/opt/anaconda3/etc/profile.d/conda.sh" ]; then
# . "/opt/anaconda3/etc/profile.d/conda.sh"  # commented out by conda initialize
        CONDA_CHANGEPS1=false conda activate base
    else
        \export PATH="/opt/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda init <<<

.zshrc was empty for me but adding still worked.

.zshrc 对我来说是空的,但添加仍然有效。

回答by Kris

If your default shell is sh (or possibly anything but bash) you won't be able to access your Anaconda python. If this is your case:

如果您的默认 shell 是 sh(或者可能是 bash 以外的任何东西),您将无法访问您的 Anaconda python。如果这是您的情况:

  1. Go to Terminal/Preferences
  2. Find 'Shells open with:'
  3. Click the button for 'Command (complete path)'
  4. Type /bin/bashas path
  1. 转到终端/首选项
  2. 找到“贝壳打开方式:”
  3. 单击“命令(完整路径)”按钮
  4. 输入/bin/bash路径

Restart your terminal. When you type $ which pythonyou should now see the anaconda python. For me it was /Users/myname/anaconda3/bin/python.

重启你的终端。当您键入时,$ which python您现在应该会看到 anaconda python。对我来说是/Users/myname/anaconda3/bin/python

$ echo $PATHwill also change now to show to correct path, with anaconda first:

$ echo $PATH现在也将更改以显示正确的路径,首先使用 anaconda:

/Users/myname/anaconda3/bin:/Users/myname/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin

/Users/myname/anaconda3/bin:/Users/myname/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin

In Atom I had to add a shebang to the beginning of each script to set this as my preference: #!/Users/myname/anaconda3/bin/python

在 Atom 中,我必须在每个脚本的开头添加一个 shebang 以将其设置为我的偏好: #!/Users/myname/anaconda3/bin/python