How do you switch between python 2 and 3, and vice versa?

I am reading How To Learn Python The Hard Way, which uses 2. Recently discovered Invent With Python, which uses 3.

Can I download python 3, and use it when I read Invent With Python, then switch back to python 2 when I want to read How To Learn Python The Hard Way. If so, how would I choose which version I use?


Solution 1:

Using 'virtualenv' you can have different isolated Python environments on a single machine. Also you can switch any-time between the different python interpreter versions.

What is virtualenv?

A Virtual Environment is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects. It enables multiple side-by-side installations of Python, one for each project. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated.

How to install?

pip install virtualenv

To create virtual environment for python 2.7 :

root:~# which python2.7

/usr/bin/python2.7

root:~# which python3.4

/usr/local/bin/python3.4

You can also use a Python interpreter of your choice:

root:~# virtualenv -p /usr/bin/python2.7 Vpy27

Running virtualenv with interpreter /usr/bin/python2.7

New python executable in /root/Vpy27/bin/python2.7

Also creating executable in /root/Vpy27/bin/python

Installing setuptools, pip, wheel...done.

To begin using the virtual environment, it needs to be activated:

root:~# source Vpy27/bin/activate

The name of the current virtual environment will now appear on the left of the prompt:

(Vpy27) root:~# python -V
Python 2.7.3

Install packages as usual, for example:

(Vpy27) root:~# pip install junos-eznc    >> All pip installs done here, will be available only in this environment.

If you are done working in the virtual environment for the moment, you can deactivate it:

(Vpy27) root:~# deactivate   

To create virtual environment for python 3.4:

root:~# which python3.4

/usr/local/bin/python3.4

root:~# virtualenv -p /usr/local/bin/python3.4 Vpy34

root:~# source Vpy34/bin/activate

(Vpy34) root:~# python -V
Python 3.4.4

There is also a way to create virtual environment with already available site-packages.

Solution 2:

depends on your system/platform...

I'm currently on Ubuntu 10.10 and have both 2.6 and 3.1 installed. The default system python is 2.6, and python3 is installed as an additional package.

corey@studio17:~$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
corey@studio17:~$ python3
Python 3.1.2 (release31-maint, Sep 17 2010, 20:27:33) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

similarly, on Windows, I have 2.6 and 3.1 installed (in C:\Python26 and C:\Python31)

easy to switch back and forth.


also.. there are some syntactic differences between 2.x and 3.x that you will need to be aware of (print is a function, etc).

Solution 3:

In windows 10 it is a lot easier then what have been given by users above.

Install both the version in separate folders, and then go to environment variable and add the path for both the versions.

Now any time you want to run particular version, just change its order (path) and move it to top of other version, and then restart the cmd and type python this time, you will see that only that particular version of python will run.

How to switch between python 2 and 3

For example in my case, I have two version of python one in anaconda(v3.0.6) and another is python 2.7

anytime I want to run the 2.7 i move its path above the anaconda version, as you can see in the screenshot above, and move it below when i want to run anaconda version.