Mac using default Python despite Anaconda install

2020-05-11 20:36发布

问题:

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:

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:

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.

Thank you

回答1:

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:

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

comes before:

$HOME/anaconda/bin

So make sure that the anaconda directory is the first one, meaning that it will have precedence:

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


回答2:

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:

# 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:

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


回答3:

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

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


回答4:

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.

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.

Regards,

JF



回答5:

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!



回答6:

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

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

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

or

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


回答7:

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

  1. vim .bash_profile => here you should see at the bottom these comments
    added by Anaconda3 2019.10 installer
  1. Add everything in between these comments to the bottom of your .zshrc file and restart your terminal.


回答8:

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

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.