6

I am trying to make a python script executable. The script is an testHelloWorld.py

#!/usr/bin/env python
print 'Hello World'

I have made it executable by running

chmod +x testHelloWorld.py

$ python testHelloWorld.py prints "Hello World". But $ ./testHelloWorld.py doesn't do anything. What am I missing here? I am using a Mac Os X device and its running Python 2.7.5.

I have gone through the answers for earlier questions and have checked for mistakes, but still no luck. This is one such similar post - how to make python script self-executable

7
  • so no output at all and no errors? Commented Jun 17, 2014 at 15:00
  • 5
    Hmm I don't know what you're missing because i've just replicated this and it works fine. What happens if you type which python and which env. Also try env python and check that it opens a python interpreter for you Commented Jun 17, 2014 at 15:00
  • What happens when you replace the first line with #!/usr/local/bin/python? Commented Jun 17, 2014 at 15:01
  • Run /usr/bin/env python in bash. Does it start python or give you an error? Commented Jun 17, 2014 at 15:02
  • /usr/bin/env python is not a valid binary directory. Here you should write path to python interpreter binary file Commented Jun 17, 2014 at 15:08

3 Answers 3

7

On my mac:

#! /usr/bin/python
print 'Hello world'

Then

chmod +x <filename>.py

and finally

$ ./<filename>.py

gives me...

Hello world

So it is just the first line. Change to #! /usr/bin/python

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

11 Comments

Tried it, still no luck. I have tried it on a MacPro and a Macbook, and got the same result. Does anyone think it might be related to the env setup? if yes, what might be the problem.
Really?? Huh. So you have a file called something.py and in it is exactly what I put in the first code section above. (with the /usr/bin/python). What is the output of which python?
$ which python /usr/bin/python $ which env /usr/bin/env
This script works fine on my linux desktop when I run it as an executable.
When you say (above) "it doesn't do anything" does it literally do nothing? It just returns with no errors etc? Does it hang and you have to ctl-c it? What about if you do sudo ./<filename>.py? (You don't want to do sudo generally.) Is it a permissions issue? Super confused. Have you tried this in different directories (e.g., /tmp)? Is this an issue with that one directory?
|
0

Yo need to find where is located the python interpreter. Write:

which python

For me python interpreter is located at /usr/local/bin/python. So the hedaer on the python file should be that one (for me).

#!/usr/local/bin/python

After that change and making executable a python file by (chmod +x filename.py) you will be able to execute a python file by writing:

./filename.py

Comments

-1

On OS X, try to replace ".py" extension by ".command" ! I don't remember why but it works for me.

Comments

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.