[Tutor] i can't for the life of me get "#! /usr/bin/env python" or "#!/usr/bin/python" to work

Lie Ryan lie.1296 at gmail.com
Wed Oct 21 22:24:57 CEST 2009


Jason Willis wrote:
> hi everyone,
> 
> sorry for the rather boring question but i'm having serious issues 
> getting my programs to run from the command line without having to type 
> "python" in-front of them. I've tried a lot of different variations on 
> the #!/usr/bin/ etc. line and have come up with the following every time:
> 
> *[root at localhost moonshinerat]# mycode.py
> bash: mycode.py: command not found
> [root at localhost moonshinerat]# mycode
> bash: mycode: command not found
> [root at localhost moonshinerat]#
> 
> I've chmod'ed the program and everything but i still get command not 
> found from the shell. The only thing that does work is ./mycode.py but 
> from what i understand that's been built into linux itself to work that 
> way...
> 
> please someone let me know what i'm doing wrong here and possibly help??
> 

No, you're not doing anything wrong. It is just the way the hashbang 
line and PATH environment variable works.

The line

$ ./script.py

will find an executable file named script.py in the *current working 
directory* (i.e. "."); and pass it the the interpreter defined in the 
hashbang line.

The line

$ script.py

will find an executable file named script.py in the PATH environment 
variable and pass it to the interpreter defined in the hashbang line.

In both cases, the hashbang line is used to find the interpreter.

As others have said, it is possible to add "." to PATH to make Linux 
works like Windows (or rather bash to work like cmd), but this is not a 
good idea for security since it makes it trivial for hacker to trick 
users (esp. root) to run a script named similar to a system executable.



More information about the Tutor mailing list