0

So, I created a simple python module, test.py

import commands

def main():
  cmd = 'ls -l'
  (status, output) = commands.getstatusoutput(cmd)
  print status, output

if __name__ == '__main__':
  main()

When I ran it using "Python test.py", I got the result that I expected. But when I ran it as an executable (yes, it has the 'x' permission), the program didn't respond at all and I had to Ctrl+C to quit it. Why is that? Shouldn't both ways give the same result?

1
  • 2
    Is #!/usr/bin/python the first line of your script? If in windows it should be something like #!C:\[python_dir]\python.exe I think. Commented May 3, 2011 at 18:45

2 Answers 2

6

Add a hash-bang line to the top:

#!/usr/bin/env python

import commands
...

This tells your system what interpreter to use to execute the script. Without it it doesn't know if it's a shell script, Perl script, Python script, what.

Sign up to request clarification or add additional context in comments.

Comments

2

You need the hashbang to be the first line of your script, referencing the path of the Python interpreter. Otherwise, all the OS knows is that you're trying to execute a script, and it has no idea how to go about doing that.

1 Comment

in fact it doesn't even know that it is a script. It could be any kind of thing that is executable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.