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
3 Answers 3
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
11 Comments
which python?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?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
On OS X, try to replace ".py" extension by ".command" ! I don't remember why but it works for me.
which pythonandwhich env. Also tryenv pythonand check that it opens a python interpreter for you#!/usr/local/bin/python?/usr/bin/env pythonin bash. Does it start python or give you an error?