8

Currently when I use "python" command, it points to python2.6. I have installed python3.1 and I want the "python" command point to python3.1. How it is possible?

mahmood@mpc:~$ which python 
/usr/bin/python
mahmood@mpc:~$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 2010-11-24 16:14 /usr/bin/python -> python2.6
mahmood@mpc:~$ uname -a
Linux orca 2.6.32-24-server #39-Ubuntu SMP Wed Jul 28 06:21:40 UTC 2010 x86_64 GNU/Linux
3
  • 1
    You need to change your environment's PATH variable. Depending on what platform you are on, this is changed differently, but either way the point is to list the Python3 path before the Python2 path. Commented Oct 6, 2011 at 19:49
  • 1
    Which OS? On linux you probably don't want to do that Commented Oct 6, 2011 at 19:50
  • I have edited the post. please check that again :) Commented Oct 6, 2011 at 19:54

7 Answers 7

16

Since you have Linux, and if you want to simply type "python" instead of "python3" in order to run Python programs, a solution is simply to define an alias in you shell configuration file (.bashrc, etc.). For Bourne shells, it should be something like

alias python=python3

(or whatever your Python 3 name is).

This way, you do not have to change anything on your system, so this solution should quite innocuous and it should not break your system.

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

Comments

10

You really don't want to change what python points to, because some programs might expect Python 2, and break.

The solution is to use virtualenv: create an isolated Python 3 environment (with the -p python3 option), activate it, and you're good to go.

2 Comments

Correct system tools use #!/path/to/python shebang. So they don't break. But you're right python should start 2.x python executable.
True. egrep -R '#! */usr/bin/env +python' /usr/bin tells me that on Ubuntu, system tools are generally immune. But affected programs include scons, hg-ssh or purple-url-handler, and worse quodlibet which is generally not started from a terminal (so I imagine it'd fail silently). I changed “system tools” to “programs”.
7
unlink /usr/bin/python
ln -s /usr/bin/python3.1 /usr/bin/python

1 Comment

unlink isn't actually necessary. Use the force! (...flag) ln -sf /usr/bin/python3.1 /usr/bin/python
4

It is not advisable.

You could write at the top in your own script (a shebang):

#!/usr/bin/env python3

If you're on Windows then install pylauncher. It understands #!.

On Linux to make your script executable, run once:

$ chmod +x your-script

After that, to run your script:

$ ./your-script

For interactive use you could create virtualenv as @Petr Viktorin points out. To install/upgrade (versions from Ubuntu's repositries are too old):

$ pip install -U virtualenv{,wrapper}

Follow instructions in /path/to/virtualenvwrapper.sh, to create virtualenv that uses python3:

$ mkvirtualenv --python python3 py3

To activate virtualenv:

$ workon py3

In an active virtualenv python refers to /path/virtualenv/bin/python. So you could run:

$ python your_module.py

4 Comments

In this case, do not start the program like python myapp.py, but rather execute it as a normal program like ./myapp.py
@Mika: For interactive command-line virtualenv should be used as @Petr Viktorin points out. Therefore python myapp.py will use python3.
Ah, so the virtualenv would override the shebang? Good to know.
@Mika: python myapp.py always uses whatever python currently refers to; regardless of shebang; whether virtualenv is active or not. As you wrote: shebang is used on Linux only then you run the script as your-script: example
3

You could follow this procedure:

sudo rm /usr/bin/python

sudo ln -s /usr/bin/python3.1 /usr/bin/python

But as already stated by Petr Viktorin, any programs that would expect python v2 would stop to work. So use with caution. You can undo the change by running:

sudo rm /usr/bin/python

sudo ln -s /usr/bin/python2.6 /usr/bin/python

Comments

2

On Linux/Mac OS you can use python3 instead of python.

3 Comments

This isn't quite true for all platforms. For instance, on Windows the Python3.x program is just called python.exe.
I would appreciate an edit with correction of my oversight more than a down vote.
Just so you know, I wasn't the one who down voted you. This answer shouldn't be in the negatives. +1
0

You could use update-alternatives.

Use the --list option to see what it's installed:

# update-alternatives --list python
/opt/python3.1/bin/python3.1
/usr/bin/python

To update the alternatives table do (assuming /opt/python3.1/bin/python3.1 is the location where your python3.1 is istalled):

# update-alternatives --install /usr/bin/python python /opt/python3.1/bin/python3.1 2

The last 2 in the previous command specifies the priority. The highest number is set as the default in case there's no manual selection.

To switch between the listed Python alternatives:

# update-alternatives --config python

This command will prompt you to enter a selection number to choose the desired Python version.

If you want to change the Python version on a per-user basis, you can create an alias in your ~/.bashrc file. E.g:

$ alias python='/opt/python3.1/bin/python3.1'

After adding the alias, re-login or source your .bashrc file:

$ . ~/.bashrc

This will set the specified Python version as the default for your user

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.