57

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.

behzad.nouri
  • 74,723
  • 18
  • 126
  • 124
  • 3
    Why don't you just change the shebang? – Tom Zych Aug 30 '11 at 00:12
  • 2
    Keep in mind that Arch Linux is one of the very few distributors of Python that has made `python` be `python3`. This has been a controversial move in the Python world. See the discussions about the draft PEP 394 (http://www.python.org/dev/peps/pep-0394/). – Ned Deily Aug 30 '11 at 00:39

9 Answers9

84

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!

Community
  • 1
  • 1
Mike
  • 1,784
  • 1
  • 12
  • 12
  • 5
    Thanks, good, simple idea. Sometimes there's hundred of python scripts running eachother and whatnot in a big build system, and they all have env python, so changing every file is not an option. – odinho - Velmont Nov 08 '12 at 16:42
  • 1
    Ditto. Lots of open source packages assume python refers to python2. This is a simple way to get a bash or zsh to map python2 to python while the package builds. The current version of virtualenv changes the prompt so there is a nice visual reminder to run deactivate when you are done. – WeakPointer Jan 14 '14 at 16:22
  • In my case, after running this the script still uses python3. – Lerk Feb 27 '19 at 11:06
  • Just a reminder for rookies like me, you always need to be in `/usr/bin` in order to run any of the above commands, so remember to do `cd /usr/bin` every time if you want to use the virtual environment. – AlienKevin May 28 '19 at 23:37
21
mkdir ~/bin
PATH=~/bin:$PATH
ln -s /usr/bin/python2 ~/bin/python

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

Doug Richardson
  • 10,483
  • 6
  • 51
  • 77
11

You could use alias python="/usr/bin/python2.7":

bash-3.2$ alias
bash-3.2$ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
bash-3.2$ alias python="/usr/bin/python3.3"
bash-3.2$ python
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 16 2013, 23:39:35) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
11

If you have some problems with virtualenv,

You can use it:

sudo ln -sf python2 /usr/bin/python

and

sudo ln -sf python3 /usr/bin/python
11

Just call the script using something like python2.7 or python2 instead of just python.

So:

python2 myscript.py

instead of:

python myscript.py

What you could alternatively do is to replace the symbolic link "python" in /usr/bin which currently links to python3 with a link to the required python2/2.x executable. Then you could just call it as you would with python 3.

o1iver
  • 1,805
  • 2
  • 17
  • 23
11

You don't want a "temporary default Python"

You want the 2.7 scripts to start with

/usr/bin/env python2.7

And you want the 3.2 scripts to begin with

/usr/bin/env python3.2

There's really no use for a "default" Python. And the idea of a "temporary default" is just a road to absolute confusion.

Remember.

Explicit is better than Implicit.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • From the default installation, wouldn't it be `python27` and `python32`? – Edwin Aug 30 '11 at 00:15
  • 1
    @Edwin: Not on my computer. It's `/usr/bin/env python2.7`. – S.Lott Aug 30 '11 at 01:35
  • 8
    Question did not say that python scripts are created by user: they may be third party scripts, and in that case we do not want to change all of theirs shebangs, especially if they are under some version control and our changes will get erased when we update the scripts. In such case we really do need to somehow temporarily set python2 as python. Good example of this is nacl sdk from google, which causes problem on archlinux because it expects python to be python2. – Martinsos Apr 01 '15 at 14:50
  • I would rather use python2 and python3 – Slava Dec 23 '15 at 14:15
4

Use python command to launch scripts, not shell directly. E.g.

  python2 /usr/bin/command

AFAIK this is the recommended method to workaround scripts with bad env interpreter line.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
3

As an alternative to virtualenv, you can use anaconda.

On Linux, to create an environment with python 2.7:

conda create -n python2p7 python=2.7
source activate python2p7

To deactivate it, you do:

source deactivate

It is possible to install other package inside your environment.

supercheval
  • 357
  • 3
  • 8
2

I think it is easier to use update-alternatives:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 
Mahmoud Kassem
  • 409
  • 5
  • 9