56

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

intl
  • 2,753
  • 9
  • 45
  • 71
  • 1
    PATH is search from left to right so the second PATH setting does you no good try with just the first – mmmmmm Mar 31 '14 at 22:32
  • 2
    Note: Apple changed the default shell from bash to zsm since Mac OS 10.15 Catalina. For readers on a more recent OS, make these changes in `.zprofile` (or `.zshrc`) instead of `.bash-profile`. – fpersyn Mar 22 '20 at 13:10

9 Answers9

64

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"
Heap
  • 946
  • 11
  • 12
  • 2
    Thanks! Works like a charm. Stupid of me to not think of it. – intl Mar 31 '14 at 22:48
  • @Heap Elegant solution. If we ever wanted to "undo" this and revert back to using the default installation, what would be the approach? – Pyderman Sep 23 '15 at 20:41
  • @Pyderman Removing the "export PATH" line that was added will suffice. – Heap Sep 25 '15 at 20:56
  • 3
    On my mac, anaconda was not under `$HOME/anaconda`, but under `//anaconda`. – arun Apr 06 '17 at 17:17
  • ^ If the other solutions provided don't add the ananconda binaries to the path this is usually the other case. – SeedyROM Oct 07 '17 at 06:46
  • On my mac recent installation of "Anaconda3-2018.12" was installed under /anaconda3 - hence starting with $HOME did not work - solution was export PATH="/anaconda3/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH". – Hari Palappetty Apr 02 '19 at 15:40
  • Thanks so much. This worked great. Conda automatically modified my .zshrc when installing and added an export path. However, as you said, this path was appearing at the end of all possible paths. I needed to move it to the beginning of my paths. – Eric Wiener Mar 12 '20 at 21:18
12

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:

  1. Go to Terminal/Preferences
  2. Find 'Shells open with:'
  3. Click the button for 'Command (complete path)'
  4. Type /bin/bash as path

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

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

/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

Kris
  • 351
  • 3
  • 5
  • 1
    This worked for me! It is weird that there is a terminal type dependency on reaching anaconda python version.. Why is that? – Enis Arik Jul 07 '20 at 16:01
  • I accidentally edited the path in Step 3/4 here and couldn't figure out what the path was. That is, I changed the path shown in Terminal Settings. I then couldn't run my python scripts as Terminal defaulted back to MacOS default which did not have any of my packages and was still on 2.7. This worked for me! Thank you. – grantog Feb 03 '21 at 19:48
  • amazing! looked everywhere for that. – Moran Reznik Apr 10 '21 at 09:15
4

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"
sushmit
  • 4,369
  • 2
  • 35
  • 38
3

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
Zhang Buzz
  • 10,420
  • 6
  • 38
  • 47
3

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"
russellthehippo
  • 402
  • 4
  • 10
2

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!

Reihan_amn
  • 2,645
  • 2
  • 21
  • 21
1

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

1

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.
Andrei
  • 51
  • 4
1

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.

ethanc9931
  • 11
  • 1