Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installed Anaconda for python 2 and 3. Can't run 2

I installed both versions of Anaconda for Python 2 and 3.

I added path.

I use Anaconda prompt.

python program.py

It assumes I am using Python 3. For example, it can't understand:

print "hello!"

I tried:

py program.py
py -2 program.py

But they didn't work. How can I run a Python 2 program?

If I type:

python

in the Acaconda prompt, then it says:

python 3.5.1. Anaconda 2.4.1

So the prompt is assuming I am using Python 3.

But I have programs written in Python 2 AND programs written in Python 3.

I want to switch between the two smoothly. How should I do this?

like image 831
user42459 Avatar asked Dec 26 '15 05:12

user42459


People also ask

Can I install Anaconda 2 and 3 together?

Regardless if you have Anaconda2 or Anaconda3 installed, you can run both python2 (2.7) and python3 (3.6) under both. You can create environments for both python versions and swap between them. You can also have both Anaconda2 and Anaconda3 installed at the same time.

Can you have two versions of Anaconda installed?

can I install multiple versions of Anaconda? You can but because of the answer above you don't need to and shouldn't. Instead of multiple Anaconda versions, just create multiple environments with the versions of packages you need. what is the difference between jupyter notebook & jupyter lab?

Does Anaconda use python 2 or 3?

Conda treats Python the same as any other package, so it is easy to manage and update multiple installations. Anaconda supports Python 3.7, 3.8, 3.9 and 3.10. The current default is Python 3.9.


1 Answers

The simples solution is to create an environment for Python 2:

conda create -n py27 python=2.7 anaconda

This will take a few seconds and will show you a ling list of packages it is going to install. Just press enter and wait.

Once finished activate your new environment.

On Windows:

activate py27 

On other platforms:

source activate py27 

The prompt should change to (py27). Now you use Python 2.7.

Deactivate with:

deactivate 

Now you are back to Python 3. You can run both versions in two terminal windows at the same time. You can create environments for other Python versions:

conda create -n py34 python=3.4 anaconda
conda create -n py35 python=3.5 anaconda

If you don't want the full Anaconda installation in an environment, create one with all packages:

conda create -n myenv python=3.5
source activate myenv

and install what you need. For example:

conda install numpy pytables sympy

You can define a bash function in your .profile or .bashrc:

py27() {
exec &>/dev/null
source activate py27
exec &>/dev/tty
python $*
exec &>/dev/null
source deactivate
exec &>/dev/tty
}

Now you can run your program with Python 27, provided you created an environment with this name using Python 2.7:

py27 myscript.py

You can generalize further an create a command for Python 2.6, 2,7, 3.3, 3.4, and 3.5:

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


py26()
{
    pyxx py26 $*
}

py27()
{
    pyxx py27 $*
}

py33()
{
    pyxx py33 $*
}

py34()
{
    pyxx py34 $*
}

py35()
{
    pyxx py35 $*
}

This assumes you already created environments with the according names and Python versions.

like image 179
Mike Müller Avatar answered Sep 17 '22 22:09

Mike Müller