Mac using default Python despite Anaconda install
PythonMacos.Bash ProfileAnacondaCondaPython Problem Overview
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
Python Solutions
Solution 1 - Python
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 - Python
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:
- Go to Terminal/Preferences
- Find 'Shells open with:'
- Click the button for 'Command (complete path)'
- 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 - Python
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 - Python
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 - Python
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!
Solution 6 - Python
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"
Solution 7 - Python
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
Solution 8 - Python
For newer versions of mac OS anaconda is now installed under ~/opt/anaconda. If you have my zsh follow these steps:
- vim .bash_profile => here you should see at the bottom these comments
added by Anaconda3 2019.10 installer
- Add everything in between these comments to the bottom of your .zshrc file and restart your terminal.
Solution 9 - Python
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.