Enforce a shell script to execute a specific python version

11,567

Solution 1

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

Solution 2

Why don't just do

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

in the last line. That should solve it (when the shebang is correct).

Share:
11,567

Related videos on Youtube

Slimjimmy
Author by

Slimjimmy

Updated on September 18, 2022

Comments

  • Slimjimmy
    Slimjimmy over 1 year

    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?

    • heemayl
      heemayl about 9 years
      Please edit your question and add the mentioned shell script..
    • muru
      muru about 9 years
      IIRC PYTHONPATH is not the path to the python executable but the path to python modules. What does the shebang of the python script contain?
    • muru
      muru about 9 years
      What is the output of head -1 /home/user/breve_2.7.2/bin/breve /home/user/breve_2.7.2/demos/Getting-Started/RandomWalker_ve‌​rsion.py?
    • Slimjimmy
      Slimjimmy about 9 years
      This line launchs a program called "breve" that runs a python script called "RandomWalker_version.py". Breve runs the script using python 2.4.3 however I want to run it using 2.7.
    • don.joey
      don.joey about 9 years
      Is the script executed or imported?
  • Ahti Komu
    Ahti Komu about 9 years
    Notice that the script file is an argument for breve binary (en.wikipedia.org/wiki/Breve_%28software%29). Therefore the script doesn't launch the python interpreter. It might be that the binary uses default python interpreter internally for executing scripts. At least that is my impression since shebang isn't working in this case.
  • Slimjimmy
    Slimjimmy about 9 years
    I have changed the update-alternatives as requested to give python2.7 priority over other versions however, when I run the script through the breve binary, its still defaults to the python 2.4.3 interpreter. Any other ideas?
  • Ahti Komu
    Ahti Komu about 9 years
    This is starting to sound like Breve uses its own python interpreter internally or it directly explicitly fires up python2.4 binary as its interpreter - most likely it's using its own interpreter. Right now I'd contact Breve author if I were you (spiderland.org/breve/documentation.php). Consult back here if you gain any insight.