Ubuntu – Enforce a shell script to execute a specific python version

bashcommand linepythonscripts

Update.

Changing the alias unfortunately does not work. I have changed the alias to alias python='/usr/bin/python2.7' however the shell script still runs the python script in 2.4.3.

Muru – I am using PYTHONPATH to direct to the python path. However as you said this may not be correct. Is there a version of PYTHONPATH that can be used to direct to the a specific python executable?

The Shell script is below as requested.

cd ../../../..

export BREVE_CLASS_PATH=/home/user/breve_2.7.2/lib/classes
export PYTHONPATH=/usr/bin/python2.7


cd /home/user/breve_2.7.2

./bin/breve /home/user/breve_2.7.2/demos/Getting-Started/RandomWalker_version.py

I am running a shell script that runs a python script in a certain program.

My problem is the python script is being launched in python 2.4 whereas I need it to run in python 2.7. In the shell script I have added the following line to try to enforce python2.7 to be used.

export PYTHONPATH=/usr/bin/python2.7

However when the python script prints what version it is using I get python 2.4.3. Am I going the correct way about this?

How should I proceed?

Best Answer

Since you have multiple python versions installed and you want to determine which python is to be used as default, you should use update-alternatives command which maintains symbolic links determining default commands.

First of all run this:

update-alternatives --list python

If the result is:

update-alternatives: error: no alternatives for python

Then you should use update-alternatives to --install alternatives of the various python versions that you have (if the --list option resulted in listing alternatives, jump straight to the --config option mentioned later). Parameters for the --install option are group, target and priority where the greater priority number results into higher priority and group means the path of the command that will be given a group of alternatives:

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

After this, python 2.7 is your default python since it was given a greater priority number and you have both python versions installed as alternatives (or more if you installed other versions too). Bear in mind that python 2.7 is now the default python for everything.

You can now list the installed alternatives again for a group with --list parameter:

update-alternatives --list python

/usr/bin/python2.4
/usr/bin/python2.7

And now you can switch between the alternatives with:

update-alternatives --config python

Enter the selection number and you're all set to have the desired version of python used as the default python.

Use man pages to read more about update-alternatives:

man update-alternatives
Related Question