1

I have two versions of python installed on my Linux box 2.6 and 2.7, as indicated in the link I have installed python 2.7 in /usr/local/bin/ and have created alias and updated PATH variable. The 2.6 is installed in /usr/bin/.

After this when I check the python version it displays 2.7.3 on terminal but on the same terminal when I run a bash script (that need to detect the python version) it displays as 2.6.

How should I enforce the bash script to refer to alias or /usr/local/bin for picking the right python version.

Community
  • 1
  • 1
Panch
  • 1,097
  • 3
  • 12
  • 43
  • please provide the bash script – piyushj May 24 '16 at 11:37
  • By default it looks like your script is looking at `/usr/bin/python` which has to be a symbolic link. Update that link to point to 2.7 directory and it should be all set. – ring bearer May 24 '16 at 11:37
  • 1
    I strongly recommend to use [`virtualenv`](https://virtualenv.pypa.io/en/stable/) – hek2mgl May 24 '16 at 11:42
  • @Panch you don't need to make changes in script file. Just set the path like I have shown and then run the script. – khrm May 24 '16 at 11:46
  • Here is the script `# Validate the python installed if [[ $(python --version 2>&1) != *2\.7* ]]; then echo -e "$COL_RED Qt compile requires Python 2.7 $COL_RESET"; exit 1 else echo "Detected Python version 2.7" fi` – Panch May 25 '16 at 02:11

2 Answers2

3

You have an alias in your profile. However, aliases do not carried along when running a script.

So what you need to do is to either use the full path everywhere in the script or to indicate the path in the very beginning.

#!/bin/bash

PYTHON_PATH=/usr/local/bin
MY_PYTHON=$PYTHON_PATH/python2.7.3

And then call it like:

$MY_PYTHON ... things
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Usually you'll use `virtualenv` for dealing with multiple versions of python. – hek2mgl May 24 '16 at 11:43
  • @hek2mgl yep! Or even docker. However, in the scope of a script I don't know if it is practical to use so: will the script get to run the Python described by the active virtualenv? – fedorqui May 24 '16 at 12:27
  • 1
    I would, and did so in the past, `source /path/to/virtualenv_name` on top of the script. Some python based programs, for example uwsgi, even support the path to a virtualenv as a startup option – hek2mgl May 24 '16 at 12:39
  • @hek2mgl excellent! Then I suggest you to post this idea as an answer. – fedorqui May 24 '16 at 12:44
  • Actually it is `source /path/to/virtualenv_name/bin/activate` ... No, it's fine, your answer covers the things behind *this* problem. – hek2mgl May 24 '16 at 12:55
  • @hek2mgl I appreciate for the hint on `virtualenv` but as of now I would just go ahead with hard coding the path. – Panch May 25 '16 at 02:23
0

You need to update your PATH. Then you can use, like this:

export PATH="/usr/local/bin:$PATH"

Note I am including usr/local/bin before the other path Include path before it. Include this in ~/.bashrc or ~/.bash_profile so that you don't need to do this every time you open a new shell.

khrm
  • 5,363
  • 1
  • 22
  • 26
  • I had already set my `PATH` and root's .bashrc as you indicated. The following is the echo of my `PATH` `# /usr/local/bin:/opt/rh/devtoolset-2/root/usr/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/panch/bin`. Despite that I was running in to that case which I have described in my initial question. – Panch May 25 '16 at 02:17
  • @Panch What's your distro? I am not able to reproduce that at all. – khrm May 25 '16 at 03:18
  • I use `rhel 6.7` evaluation version. `The uname -r` gives `2.6.32-573.el6.x86_64` – Panch May 25 '16 at 03:35
  • @Panch And you run this script from root user? Or normal user? – khrm May 25 '16 at 03:36
  • I'm running it as a `root` user, and have included the alias in the root's `.bashrc` – Panch May 25 '16 at 03:52
  • Have you put alias? Or Exactly what I have written? – khrm May 25 '16 at 04:55
  • Yes I have done that. The content of my .bashrc is `alias python=/usr/local/bin/python2.7 alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' alias vi=vim alias wkgdir='cd ~/Perforce/t_murup_Panch-Linux_4292/' export PATH="/usr/local/bin:$PATH" # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi ` – Panch May 25 '16 at 06:50