Question

Python code:

"<stdin>"
#!/usr/bin/env
print "Hello world!" 
print "How are you?" 

The above is some code that I applied onto a script and it works when run in TextWrangler, but when I put it in Terminal, it fails.

Why is this happening? Does it have to do with the way I open the file?

Était-ce utile?

La solution

/usr/bin/env is not the correct path to Python. Most likely the shebang line should read:

#!/usr/bin/env python

I am pretty sure it needs to be the first line, so delete the "<stdin>" line as well (that's ignored by Python anyway).

Also make sure you have set the execute permission on the script: chmod +x /path/to/script.py

Autres conseils

The line that starts with #! is called the shebang line in Unix. By definition, two things are wrong with your shebang:

  1. The sheebang MUST be the initial line in the script -- you have it on the second line.
  2. If Python is the target interpreter, you either need the absolute path to Python (something like #!/usr/bin/Python OR an argument to env to execute the configured Python -- something like #!/usr/bin/env Python note the argument 'Python' to the utility env
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top