60

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.

2
  • 3
    Why don't you just change the shebang? Commented Aug 30, 2011 at 0: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 (python.org/dev/peps/pep-0394). Commented Aug 30, 2011 at 0:39

9 Answers 9

85

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!

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
In my case, after running this the script still uses python3.
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.
21
mkdir ~/bin
PATH=~/bin:$PATH
ln -s /usr/bin/python2 ~/bin/python

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

1 Comment

plain and simple! no need for virtualenv.
12

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

Comments

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.

Comments

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.

4 Comments

From the default installation, wouldn't it be python27 and python32?
@Edwin: Not on my computer. It's /usr/bin/env python2.7.
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.
I would rather use python2 and python3
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.
>>> 

1 Comment

Aliases aren't inherited by child shells, so this would only work when directly calling python.
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.

Comments

3

As an alternative to virtualenv, you can use conda.

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.

Comments

2

I think it is easier to use update-alternatives:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 

1 Comment

This worked perfectly for me on Ubuntu 22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.