If I have a basic Python script, with it's hashbang and what-not in place, so that from the terminal on Linux I can run
/path/to/file/MyScript [args]
without executing through the interpreter or any file extensions, and it will execute the program.
So would I install this script so that I can type simply
MyScript [args]
anywhere in the system and it will run? Can this be implemented for all users on the system, or must it be redone for each one? Do I simply place the script in a specific directory, or are other things necessary?
The best place to put things like this is /usr/local/bin
.
This is the normal place to put custom installed binaries, and should be early in your PATH
.
Simply copy the script there (probably using sudo
), and it should work for any user.
Make a python script:
cd /home/el/bin touch stuff.py chmod +x stuff.py
Find out where your python is:
which python /usr/bin/python
Put this code in there:
#!/usr/bin/python print "hi"
Run in it the same directory:
python stuff.py
Go up a directory and it's not available:
cd .. stuff.py -bash: stuff.py: command not found
Not found! It's as we expect, add the file path of the python file to the $PATH
vi ~/.bashrc
Add the file:
export PATH=$PATH:/home/el/bin
Save it out, re apply the .bashrc, and retry
source ~/.bashrc
Try again:
cd /home/el stuff.py
Prints:
hi
The trick is that the bash shell knows the language of the file via the shebang.
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