PDA

View Full Version : Excecuting a python script as a command



blithen
May 5th, 2008, 02:54 AM
Such as typing
music-collecter in the terminal just like a normal program, how do you do that?

LaRoza
May 5th, 2008, 02:55 AM
Such as typing
music-collecter in the terminal just like a normal program, how do you do that?

Mark it as executable and run it.

If it is not in your $PATH, put it there, or add the path to your $PATH. Do not add ~ to your $PATH.

(There is a link in the sticky on scripting languages, which includes marking as executable and shebangs)

blithen
May 5th, 2008, 03:03 AM
I messed up >.>

blithen
May 5th, 2008, 03:03 AM
Mark it as executable and run it.

If it is not in your $PATH, put it there, or add the path to your $PATH. Do not add ~ to your $PATH.

(There is a link in the sticky on scripting languages, which includes marking as executable and shebangs)


blithen@sparky:~/python$ sudo cp musiccollect /usr/bin/
[sudo] password for blithen:
blithen@sparky:~/python$ musiccollect
/usr/bin/musiccollect: line 1: syntax error near unexpected token `('
/usr/bin/musiccollect: line 1: `def Walk( root, recurse=0, pattern='*', return_folders=0 ):'

I get that error, I have no idea what you are talking about when you say $PATH

Can+~
May 5th, 2008, 03:29 AM
Now it will be executed directly, similar to doing


./musiccolect

So, to bash now how to execute it, you must set the first line of your application with


#!/usr/bin/env python

And by PATH it means, the places that bash usually look for executables, you can see this places doing:


echo $PATH

So yeah, you put it on /usr/bin, which is a typical place for PATH.

blithen
May 5th, 2008, 03:50 AM
Now it will be executed directly, similar to doing


./musiccolect

So, to bash now how to execute it, you must set the first line of your application with


#!/usr/bin/env python

And by PATH it means, the places that bash usually look for executables, you can see this places doing:


echo $PATH

So yeah, you put it on /usr/bin, which is a typical place for PATH.

Sweet it works, thanks!

LaRoza
May 5th, 2008, 04:38 AM
Sweet it works, thanks!

Of course, all of that was in the sticky...

mssever
May 5th, 2008, 07:11 AM
One other thing:

/usr/bin is where Ubuntu packages go. It's preferable to not pollute that namespace. Instead put stuff you write or otherwise manually add in /usr/local/bin or ~/bin. /usr/local/bin is part of the default $PATH. I'm not sure about ~/bin; you might have to manually add it.