Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aliased Linux commands are not recognized from python (commands.getoutput)

Tags:

python

linux

I'm working on Ubuntu 12.4 and I have matlab installed. Typically, in order to invoke matlab from terminal I have to type in '~/MATLAB/bin/matlab'. Obviously this is a bit annoying so I aliased this command by adding

alias matlab='sh ~/MATLAB/bin/matlab'

to .bashrc. Now everything is golden and typing 'matlab' in the terminal (bash) works from any directory.

The problem arises when I'm trying to invoke Matlab from a python script. having a statement like this:

>>> commands.getoutput('matlab')
'sh: 1: matlab: not found'

as it seems the alias is not being recognized. Just to make sure:

>>> commands.getoutput('~/MATLAB/bin/matlab')

works like a charm, and

>>> commands.getoutput('echo $SHELL')
'/bin/bash'

indeed verifies that python is trying to execute the cmd in bash...

Any idea what's happening here? why isn't the alias being recognized? how/can it be fixed?

Thanks!

like image 728
ScienceFriction Avatar asked Feb 27 '26 12:02

ScienceFriction


1 Answers

Add the matlab binary path to your PATH environment variable.

PATH=~/MATLAB/bin/:$PATH
export PATH

Then python would find matlab:

>>> commands.getoutput('matlab')

I think commands doesn't know of your shell's current aliases. However environment variables such as PATH persist.

like image 142
aayoubi Avatar answered Mar 02 '26 14:03

aayoubi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!