Use the default Python rather than the Anaconda installation when called from the terminal

90,947

Solution 1

Anaconda adds the path to your .bashrc, so it is found first. You can add the path to your default Python instance to .bashrc or remove the path to Anaconda if you don't want to use it.

You can also use the full path /usr/bin/python in Bash to use the default Python interpreter.

If you leave your .bashrc file as is, any command you run using python will use the Anaconda interpreter. If you want, you could also use an alias for each interpreter.

You will see something like export PATH=$HOME/anaconda/bin:$PATH in your .bashrc file.

So basically, if you want to use Anaconda as your main everyday interpreter, use the full path to your default Python or create an alias. If you want it the other way around, remove the export PATH=.... from bashrc and use full path to Anaconda Python interpreter.

Solution 2

Having tried all the suggestions so far, I think modifying the export statement in file ~/.bashrc, as Piotr Dobrogost seems to suggest, is the best option considering the following:

  • If you remove the whole statement, you have to use full paths for Conda binaries.
  • Using Conda 4.4.10 links in the directory anaconda/bin/ point to binaries in the same directory, not the system ones in /usr/bin.
  • Using this approach you get the system programs for all that have been previously included in $PATH and also the ones specific to anaconda without using full paths.

So in file ~/.bashrc instead of

# Added by the Anaconda3 4.3.0 installer
export PATH="/home/user/anaconda3/bin:$PATH"

one would use

export PATH="$PATH:/home/user/anaconda3/bin"

Solution 3

I faced the same issue and you can do the following.

Go into your .bashrc file and you will find a similar sort of line:

export PATH=~/anaconda3/bin:$PATH

You comment it out and instead type out:

alias pyconda='~/anaconda3/bin/python3'

Or whatever your path is. This worked out for me.

Solution 4

at 2020, like the @spacetyper mentioned, it acted differently. I've found a handy solution for that from this question: How do I prevent Conda from activating the base environment by default?

To disable automatic base activation:

conda config --set auto_activate_base false

it'll create a ./condarc in home directory after running the first time.

Solution 5

In the year 2020, Conda adds in a more complicated block of code at the bottom of your .bash_profile file that looks something like this:

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

To use the default Python install by default: Simply move this section of code to the very top of your .bash_profile file.

To give yourself the option of using the Conda installed Python: Add this line below the Conda code block above.

alias pyconda="/Users/spacetyper/opt/miniconda3/bin/python3"

Now you should be able to call the system Python install with python and the Conda install with pyconda.

Share:
90,947
Michael
Author by

Michael

Updated on July 05, 2022

Comments

  • Michael
    Michael almost 2 years

    I recently installed the Anaconda version of Python. Now when I type python into the terminal it opens the Anaconda distribution rather than the default distribution. How do I get it to use the default version for the command python on Linux (Ubuntu 12.04 (Precise Pangolin))?

  • Aditya Kashi
    Aditya Kashi about 7 years
    +1 for the alias idea. In fedora 25, I can add "alias python=/usr/bin/python", and now when I say "python" or "python2" I get the system python 2.7, but if I say "python3" I get the conda python. The advantage of this is that system tools which rely on python 2.7 (like gnome-tweak-tool) work fine.
  • Guig
    Guig about 6 years
    I also had to change my ~/.bash_profile in the same way
  • sg7
    sg7 about 6 years
    This does not seem to address the OP question.
  • Mr. Unnormalized Posterior
    Mr. Unnormalized Posterior about 6 years
    How do I use this?
  • cbcoutinho
    cbcoutinho about 6 years
    I'd recommend looking at the dotfiles I linked to above use it for a good example, but most simply you need to source the sandboxing functions in your ~/.bashrc or ~/.zshrc, and then create a sandbox for conda by placing the following after sourcing the sandboxing functions: sandbox_hook conda conda
  • P A N
    P A N almost 6 years
    This worked for me on MacOS with anaconda installed via brew cask install anaconda: export PATH="$PATH:/usr/local/anaconda3/bin"
  • toofrellik
    toofrellik about 5 years
    What needs to be changed if we have a similar issue in Windows
  • dingx
    dingx about 5 years
    Hi, conda added a section rather than a simple line: "# >>> conda initialize >>> # !! Contents within this block are managed by 'conda init' !! __conda_setup="$('/home/dingxin/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)" if [ $? -eq 0 ]; then eval "$__conda_setup" else if [ -f "/home/dingxin/anaconda3/etc/profile.d/conda.sh" ]; then . "/home/dingxin/anaconda3/etc/profile.d/conda.sh" else export PATH="/home/dingxin/anaconda3/bin:$PATH" fi fi unset __conda_setup # <<< conda initialize <<" . how to deal with it in this case?
  • mah65
    mah65 over 4 years
    Where to find .bashrc in windows?? I want to use python in cmd.
  • Kate Stohr
    Kate Stohr almost 4 years
    This worked. I am curious. Please can you explain why adding the shell command '$PATH' in front of the path rather than trailing the path prevents conda from being called as the default python interpreter.
  • Asta86
    Asta86 almost 4 years
    Because in that case the interpreter located in for instance /usr/bin is found before the one in the anaconda directory, and the first match is utilized (check baeldung.com/linux/path-variable for more details).
  • Charlie Parker
    Charlie Parker almost 4 years
    for me the first one you have works. I'm surprised you used the second one. My command: export PATH="/Users/YOURNAME/miniconda3/bin:$PATH"
  • Charlie Parker
    Charlie Parker almost 4 years
    what is wrong with doing: export PATH="/Users/YOURNAME/miniconda3/bin:$PATH"? that seemed to work for me.
  • Padraic Cunningham
    Padraic Cunningham almost 4 years
    @CharlieParker, It is exactly the same. $HOME expands to /home/user. It is just shorthand for writing it out and developers are lazy...
  • Peter Mortensen
    Peter Mortensen over 3 years
    A .bak file on Linux?
  • Peter Mortensen
    Peter Mortensen over 3 years
    Isn't a VM something else?
  • Cloud Cho
    Cloud Cho over 3 years
    @PeterMortensen You're right. I corrected to Virtual Environment.
  • HongboZhu
    HongboZhu almost 3 years
    What's annoying is, the conda pip also shadows the system pip. So if I install dependencies using (conda) pip and install another Python binding, which by default use system Python, it will fail to work. So for me changing the PATH variable is a better solution.