[Tutor] shebang problem

Jonathon Sisson sisson.j at gmail.com
Sat Nov 4 20:07:16 CET 2006


Brian,

It's not a permissions issue...

(from the original e-mail...see below)
 >> brian at gottlob:~/test$ ls -la shebangtest.py
 >> -rwxr-xr-- 1 brian brian 68 2006-11-04 02:29 shebangtest.py

This is clearly executable by brian, and clearly being executed by 
brian.  The shebang line is correct (#!/usr/bin/python).

The problem is *how* you are executing the file.  If you simply type in 
the filename, your shell looks on your PATH to find the file.  Unless 
~/test is on PATH, you'll get a "command not found" error.

Instead type in:

./shebangtest.py

and see if that works.  "./" tells the shell to look in your current 
working directory for the file.

Let me know if that solved the problem...

Jonathon


Carlos Hanson wrote:
> On Sat, November 4, 2006 4:11 pm, Brian van den Broek wrote:
>> Hi all,
>>
>> I'm still getting comfortable with Linux and this might be an OS
>> rather than a python problem.
>>
>> I am trying to make a script directly executable. I've reviewed the
>> 2nd ed of the Nutshell, and I cannot work out what I'm doing wrong.
>> I'm running ubunutu 6.10 (edgy eft). Here's a copy past of my command
>> line:
>>
>> brian at gottlob:~/test$ which python
>> /usr/bin/python
>> brian at gottlob:~/test$ ls -la shebangtest.py
>> -rwxr-xr-- 1 brian brian 68 2006-11-04 02:29 shebangtest.py
>> brian at gottlob:~/test$ cat shebangtest.py
>> #!/usr/bin/python
>>
>> if __name__ == '__main__':
>>
>>      print "It works"
>> brian at gottlob:~/test$ shebangtest
>> bash: shebangtest: command not found
>> brian at gottlob:~/test$ shebangtest.py
>> bash: shebangtest.py: command not found
>>
>> I've also tried:
>>
>> #!/usr/bin python
>>
>> as my shebang line.
>>
>> I've been unable to get this to work. Clearly, there is something I've
>> misunderstood. (`#!' is not an easy thing to google for :-)
>>
>> Best,
>>
>> Brian vdB
>>
> 
> Your answer lies in the file permission.  The file needs to be
> executable.  If you look at the man pages for chmod, you will find
> your answer.
> 
> The shebang line tells the shell what to use to run the script.  For
> example, if the file is not executable, you would execute it as
> follows:
> 
> $ python shebangtest.py
> 
> As you found with `which python`, python is in /usr/bin, so executing
> the script is actually
> 
> $ /usr/bin/python shebangtest.py
> 
> Therefore, the shebang line needs to be as follows:
> 
> #! /usr/bin/python
> 
> Then once the script has execute permissions (man chmod), it will run
> as expected.
> 


More information about the Tutor mailing list