2

Background, I am trying to use a Python library that spawns a subprocess using arguments ["bash", "-c", "python someScript.py"]. What's happening is Python 2.6 is executed, but I want python2.7 instead. Python 2.6 is in /usr/bin/ and Python 2.7 is in /usr/local/bin

If I say python -V and type python I get:

Python 2.7.10 and python is aliased to /usr/local/bin/python2.7

(Note, I have set up this alias in my .bashrc file: alias python=/usr/local/bin/python2.7)

However, if I invoke bash -c "type python" I get:

python is /usr/bin/python

Both echo $PATH and bash -c "echo $PATH" output the same path:

/usr/local/bin:/usr/local/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/lib:/usr/local/lib

Note, I tried to put /usr/local/bin first when I export PATH in my .bashrc.

So my question is, what is controlling the pathing (in this case to Python) when I execute a string command using bash -c ? Specifically, how do I get bash -c "python" to use Python 2.7? Edit: The arguments ["bash", "-c", "python someScript.py"] are hard-coded in the library, and I'd rather not have to modify the library source.

3 Answers 3

1

The problem is that while doing bash -c '....', you are spawning a non-interactive (and non-login) session of bash, which will not source any runtime configuration file e.g. ~/.bashrc.

As you have defined the alias in ~/.bashrc, you can use the -i option so that bash can simulate the interactive environment and hence source the ~/.bashrc file:

bash -ic '....'

Now the alias should be available.

Example:

$ alias l
alias l='ls'

$ bash -c 'l'
bash: l: command not found

$ bash -ic 'l'
bar  foo
0

Use the full path to the command. bash -c /usr/local/bin/python

1
  • Let me clarify that the subprocess arguments ["bash", "-c", "python someScript.py"] are hard-coded in the library. I'd like to find a solution that doesn't require modifying the library code. Nov 8, 2015 at 21:57
0

I think your .bashrc is not sourced in a non-interactive shell such as this - try putting the PATH setting into your .profile file, which will also work for some other shells (or .bash_profile if you only want to set it for bash).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .