Mac using default Python despite Anaconda install

69,055

Solution 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"

Solution 2

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

Solution 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"

Solution 4

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

Solution 5

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"
Share:
69,055
intl
Author by

intl

Updated on July 09, 2022

Comments

  • intl
    intl almost 2 years

    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
    intl about 10 years
    Thanks! Works like a charm. Stupid of me to not think of it.
  • Pyderman
    Pyderman over 8 years
    @Heap Elegant solution. If we ever wanted to "undo" this and revert back to using the default installation, what would be the approach?
  • Heap
    Heap over 8 years
    @Pyderman Removing the "export PATH" line that was added will suffice.
  • arun
    arun about 7 years
    On my mac, anaconda was not under $HOME/anaconda, but under //anaconda.
  • SeedyROM
    SeedyROM over 6 years
    ^ If the other solutions provided don't add the ananconda binaries to the path this is usually the other case.
  • Hari Palappetty
    Hari Palappetty about 5 years
    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/loca‌​l/bin:$PATH".
  • Eric Wiener
    Eric Wiener about 4 years
    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.
  • Enis Arik
    Enis Arik almost 4 years
    This worked for me! It is weird that there is a terminal type dependency on reaching anaconda python version.. Why is that?
  • Enis Arik
    Enis Arik almost 4 years
    Nope, answer of @Chris has worked for me for Python 3.
  • grantog
    grantog about 3 years
    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.
  • Moran Reznik
    Moran Reznik about 3 years
    amazing! looked everywhere for that.