문제

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?

도움이 되었습니까?

해결책

/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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top