Alex Rivera | Logout

Can I use an alias to execute a program from a python script

Asked 2011-07-28T08:34:09.250
11

I am almost brand new to python scripting, so please excuse any stupid questions, but any help anyone can give would be much appreciated.

I am trying to write a python script for other people to use, and in it I need to call a program that I won't always know the path to. To get around that, I ask the user to provide the path to the program, which will work, but I don't want users to have to provide the path EVERY time they run the script so I have been trying to set up a bash alias by having the script add to the ~/.profile and ~/.bashrc files.

I can then use the alias to run the program from an interactive bash shell, but when the script tries to run it, I get a "command not found" error...

I have tried re-sourcing the .bashrc file and using the "shopt -s expand_aliases" command with no luck.

My ~/.bashrc looks like this:

alias nuke='/Applications/Nuke6.2v4/Nuke6.2v4.app/Contents/MacOS/Nuke6.2v4'

And the piece of the script looks like this:

os.system('source .bashrc')
os.system('shopt -s expand_aliases')
os.system('nuke -x scriptPath')

But once the script gets up to this point, it returns:

sh: nuke: command not found

Am I doing something wrong or is there another way I can permanently store the path to a program?

Edit
Report

2 Answers

4

Be aware that os.system is probably using sh rather than bash, and so source and shopt will fail as well.

If it is using bash, it will fail as os.system creates a new process for each call. You could do it in one line like this:

os.system('source .bashrc; shopt -s expand_aliases; nuke -x scriptPath')

But you are by far best to get the path in some other way (or even read it from .bashrc manually if you want) and then use subprocess.Popen().

answered 2011-07-28T09:51:03.463
0

Yeah don't do that. Write your configuration out into your own dotfile, and don't use os.system, use subprocess.

answered 2011-07-28T09:15:17.290

Your Answer