Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python 2 instead of python 3 as the (temporary) default python?

Tags:

python

on my computer

~$ python -V  Python 3.2.1 

but I get into problems when I run some python programs. my guess is (or at least I want to try this) that there is some backward compatibility issues, and I want to run those python scripts with

 python2 2.7.2-2 

which is also installed on my system but I do not know how to make it as the (temporary) default python. The python script starts with

 #!/usr/bin/env python 

and I am using arch linux.

like image 927
behzad.nouri Avatar asked Aug 30 '11 00:08

behzad.nouri


People also ask

Why is Python 2 default?

The reason why Python 2 is invoked when python is run lies in the one of the historical point of PEP 394 -- The "python" Command on Unix-Like Systems: The python command should always invoke Python 2 (to prevent hard-to-diagnose errors when Python 2 code is run on Python 3).

How do I make Python 3 default to Python?

To change to python3, you can use the following command in terminal alias python=python3 . But that only work for the current running process in terminal.


2 Answers

You can use virtualenv

# Use this to create your temporary python "install" # (Assuming that is the correct path to the python interpreter you want to use.) virtualenv -p /usr/bin/python2.7 --distribute temp-python  # Type this command when you want to use your temporary python. # While you are using your temporary python you will also have access to a temporary pip, # which will keep all packages installed with it separate from your main python install. # A shorter version of this command would be ". temp-python/bin/activate" source temp-python/bin/activate  # When you no longer wish to use you temporary python type deactivate 

Enjoy!

like image 199
Mike Avatar answered Sep 21 '22 12:09

Mike


mkdir ~/bin PATH=~/bin:$PATH ln -s /usr/bin/python2 ~/bin/python 

To stop using python2, exit or rm ~/bin/python.

like image 21
Doug Richardson Avatar answered Sep 24 '22 12:09

Doug Richardson