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

Community
  • 1
  • 1
deepng
  • 809
  • 2
  • 9
  • 20

3 Answers3

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

AstroCB
  • 12,337
  • 20
  • 57
  • 73
brechmos
  • 1,278
  • 1
  • 11
  • 22
  • 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. – deepng Jun 18 '14 at 02:21
  • 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`? – brechmos Jun 18 '14 at 02:50
  • $ which python /usr/bin/python $ which env /usr/bin/env – deepng Jun 18 '14 at 04:37
  • This script works fine on my linux desktop when I run it as an executable. – deepng Jun 18 '14 at 04:39
  • 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 ./.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? – brechmos Jun 18 '14 at 12:28
  • Yes, It returns no errors. $ sudo ./testHelloWorld.py Password: $ mv testHelloWorld.py /tmp $ /tmp/testHelloWorld.py ** $ python /tmp/testHelloWorld.py hello world ** – deepng Jun 19 '14 at 06:54
  • Stumped. Wow. What is the output of `ls -asl – brechmos Jun 19 '14 at 12:51
  • ls -asl gives -> *-rwxr-xr-x 1 deepng staff 40 Jun 19 12:25 testHelloWorld.py* The bob.sh and bob.pl file executes just fine. – deepng Jun 19 '14 at 13:57
  • Huh. For fun, how about create a python script that writes something to a temporary file. Something like `fp=open('/tmp/tt.txt', 'wt')` `fp.write('Test')` `fp.close()`. Run that and see if the temporary file is created. – brechmos Jun 19 '14 at 14:56
  • Same result. The script just doesn't run. The tmp/ file is never created. – deepng Jun 20 '14 at 10:09
  • Did you ever get this running @deepng? I have the same results. – JasTonAChair Oct 17 '15 at 10:38
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
-1

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

effo
  • 1