Daniel
Daniel

Reputation: 12026

Python and python3 side-by-side on MacOS - Anaconda

I have both anaconda 2 and anaconda 3 installed.

I would like to call python3 and have python3 from anaconda 3, and call python and have python from anaconda 2.

My issue is that adding anaconda3's bin path to PATH will also add the name python, so that python from anaconda 2 gets shadowed. Is it possible to have a sane environment with both languages working side-by-side?

If I do something like:

export PATH="/Path/to/anaconda2:/Path/to/anaconda3:$PATH"

Then I get what I want, since anaconda2 will shadow anaconda3's python, but not python3. I don't know if it has any unwanted consequences.

Upvotes: 0

Views: 554

Answers (2)

Mike Müller
Mike Müller

Reputation: 85442

The best way to work with different Python versions at the same time is to create conda environments.

For Python 2:

conda create -n py27 Python=2.7

Activate with:

source activate py27

For Python 3:

conda create -n py35 Python=3.5

Activate with:

source activate py35

Deactivate with:

source deactivate

Once activated, you can install single packages or the whole Anaconda distribution:

conda install anaconda

This would allow you to create more environments say for Python 3.6 or for different combination of versions of libraries.

Add this to your .profile:

pyxx() {
exec &>/dev/null
source activate $1
exec &>/dev/tty
python ${*:2}
exec &>/dev/null
source deactivate
exec &>/dev/tty
}



py27()
{
    pyxx py27 $*
}

py35()
{
    pyxx py35 $*
}

Now calling:

py27 

would activate the environment for Python 2.7, run Python in it and deactivate the environment after the Python process is finished.

Same goes for:

py35

Of course you could (for while) call your environments python2 and python3.

Upvotes: 2

jeffknupp
jeffknupp

Reputation: 6274

You answered your own question: setting your $PATH to find the Python 2 version of Anaconda before the Python 3 version achieves exactly what you want and is the only way (short of symlinks and/or moving actual binaries around) you can get the results you want.

...and if anyone mentions "create an alias", just run.

Upvotes: 1

Related Questions