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?
To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World! If everything works okay, after you press Enter , you'll see the phrase Hello World!
An alias is a way of shortening a command. (They are only used in interactive shells and not in scripts — this is one of the very few differences between a script and an interactive shell.)
Aliasing multiple commandsYou can combine multiple commands in an alias by separating them with a semicolon, or by using &&, which will run the next command only if the previous command succeeds. This will cd to the top-level git directory, check out the main branch, and run gitpull.
I know this is an old question, but for anyone else who comes across this in the future, I think it's worth mentioning that modifying someone's ~/.bashrc or ~/.profile files (especially silently) is one of those ideas that generally falls under the umbrella of "bad practice". Additionally, it seems a bit heavy-handed for the problem you need to solve.
Instead, why not have your script keep track of its own configuration file stored in the user's home directory? It would be quite simple to do this using ConfigParser
, your own JSON structure dumped to a file, or something else entirely if you want.
Then in your script, you can first check if it exists, and if it does, see if it contains the key you're looking for that holds the executable path. If either of those tests fail, you know you need to prompt the user for the path, at which point you can write it to the config file for next time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With