linux - How to execute python script from console without writing full path?
2013-12
I have a few python scripts on /usr/share/scripts/ that I use often, and I want to be able to execute them by just writing the name and not the full path, how could I do this?
echo $PATH shows me:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/real/RealPlayer
So I tried writing on the terminal:
PATH="/usr/share/scripts/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/real/RealPlayer"
export
No errors shown and echo $PATH now shows my new scripts path, but when I run scriptName I get command not found.
What am i doing wrong?
Set executable permissions for python scripts by "chmod +x *"
Now you have two options:
- Add your scripts directory to PATH env variable, or
- Make symbolic links to your scripts one by one (or write another script to do the same) in
/usr/local/bin
directory.
Example:
[mzed@node02 test]$ vim printme.py
Contents of file:
#!/usr/bin/python
print "This is cool!"
-
[mzed@node02 test]$ mv printme.py printme
[mzed@node02 test]$ chmod +x printme
[mzed@node02 ~]$ cd /usr/local/bin/
[mzed@node02 bin]$ sudo ln -s ~/test/printme .
[mzed@node02 bin]$ ls
deskzilla grails grails-debug printme startGrails
[mzed@node02 bin]$ cd
[mzed@node02 ~]$ printme
This is cool!
[mzed@node02 ~]$
I hope this will help you.
Sorry for suggesting a basic thing.. Did you try "scriptname.py", instead of just "scriptname"?
Also, all the scripts need to have execute permissions (you can do that by issuing "chmod +x script.py").. Judging from your comment above, since you have run them like "/usr/share/scripts/scriptName.py args", they should have execute permissions.
Okay, maybe I'm just older school...
In /usr/bin add shell scripts with the #!/bin/bash header and no .sh extension. Then in those scripts just run python absolutepath.
Why I think it's better than the other answers:
Doesn't require chmod-ing your scripts to make them executable.
Doesn't require renaming your scripts.
I need to run ./pythonScript keyword one time for each keyword in a text file, how can I do this from a gnome terminal? (without having to modify the pythonScript)
pseudo code:
for each keyword in file:
./pythonScript keyword
waitfor(pythonScript to finish)
Assuming there is one keyword per line, here's a pure shell, portable solution:
while read -r line; do
./pythonScript "$line"
done <file
Here's a slightly simpler Linux solution:
<file xargs -d '\n' -n 1 ./pythonScript
Both solutions allow any character other than newline to appear in a keyword.
Is there a keyword per line from the file? If so,
while read keyword
do
./pythonScript $keyword
done < file
If you have GNU Parallel http:// www.gnu.org/software/parallel/ installed you can do this:
cat file | parallel ./pythonScript
This will run the jobs in parallel can be very useful if you run on a multicore machine.
Watch the intro video for GNU Parallel to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ