13

Possible Duplicate:
Calling a python script from command line without typing “python” first

I've tried

bash$ chmod +x script.py  

doesn't work. I also remember to put

#!usr/bin/env python  

at the beginning of the script.

bash$ ./script.py  

Does nothing, it just changes my cursor to a cross lol

UPDATE: I've fixed

#!/usr/bin/python    

i've also tried

chmod a+x script.py   

still nothing. My script has import commands and uses sys.argv...I've followed the instruction on this link (look at the end of the page). Nothing works

Community
  • 1
  • 1
BPm
  • 2,924
  • 11
  • 33
  • 51
  • 1
    What does your script actually do? Have you tried this with a hello.py? I usually use #!/usr/bin/python and chmod 755 hello.py. – Paul Nathan Sep 26 '11 at 23:59
  • 1
    bash$ chmod **a+x** script.py . Gotta' say ***who*** gets to execute the script. – Pete Wilson Sep 27 '11 at 00:00
  • 2
    Just FYI: the reason your cursor is turning into a cross is that your script is being interpreted as a shell script since you messed up the shebang (see @TokenMacGuy's answer). That means your "import" statement is running the command `import`, which takes a screenshot. You may now have a PNG file lying around named `sys`, `os`, or something similar... – Laurence Gonsalves Sep 27 '11 at 00:05
  • you're right. there is import command. how do i fix that? i already fixed the "shebang" and it's still the same – BPm Sep 27 '11 at 00:07
  • @Pete: no, you don't. Without a "who", the symbolic form of `chmod` behaves almost as though you used `a`, though it's filtered by your umask. – Laurence Gonsalves Sep 27 '11 at 00:19
  • @BPm @TokenMacGuy's answer has an extraneous space. Maybe try again? Another approach is to use `#!/usr/bin/python` (or whatever your full path to python is). It lacks some of the flexibility of the `env` approach, but perhaps it'll be easier to get working. – Laurence Gonsalves Sep 27 '11 at 00:21
  • @LaurenceGonsalves I've tried `#!/usr/bin/python` but it still turns my cursor to a cross. I really thing the problem is the import commands. For scripts without `import` works fine. – BPm Sep 27 '11 at 16:09
  • @BPm It's pretty unlikely that the import statements are your real problem. The import statements might be making the issue more *obvious*, but it really sounds like your scripts are being run as shell scripts, not Python scripts. To test, what's the output if your script contains only `print sum(range(11))`? after the shebang line? – Laurence Gonsalves Sep 27 '11 at 16:20
  • @LaurenceGonsalves ok I see. My script uses `argv` (maybe that's why it's a shell script...) so for a shell script how do i make it self-executable? – BPm Sep 27 '11 at 16:29
  • @BPm You can use `argv` in many languages, so your last question doesn't make any sense to me. Files are made executable by setting the x bit(s), like with `chmod +x`, regardless of scripting language. Before executing a script the first line is checked to see if it's a shebang (`#!...`), and if it is that command is used as the interpreter, otherwise the shell is used. Anyway, it'd be easier to debug your problem if you'd answer the question from my previous comment: what's the output if you change your script that "works fine" to contain only `print sum(range(11))` after the shebang? – Laurence Gonsalves Sep 28 '11 at 21:50
  • @LaurenceGonsalves yes. it works fine for `print sum(range(11))` .i dont know what happened but after i wrote my script on eclipse it works fine now. Probably because of my text editor. – BPm Sep 28 '11 at 23:47

2 Answers2

18

Here is the list of things to try, in rough order of likelihood:

  • Ensure that the shebang line has correct syntax (you've done this already, #!/usr/bin/python).
  • Make sure the shebang is the first line in the file (not even a blank line or a comment above it).
  • Verify that /usr/bin/python actually exists and works. Your Python interpreter may be installed elsewhere. Type /usr/bin/python at a prompt and make sure Python starts. Type which python if you don't know where it is installed.
  • If . is not in your PATH (it may not be), you must run your script with ./script.py because the shell does not look for commands in the current directory by default.
  • Make sure the executable bit is set on your script (+x, verify with ls -l).
  • Make sure that you are using LF only line endings in your editor. Shells can be picky, and your shebang line must end with LF only and not CRLF. This is only likely to be a problem if you're using a Windows text editor, but it might be worth checking.
  • Make sure that your text editor does not silently insert a UTF-8 BOM at the start of the file. Again, this is only likely if you're using Notepad on Windows.
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
11

the "shebang" needs to contain the full path to the executable. You're calling env, which is good, but you haven't given it the full path: start your script like so:

#!/usr/bin/env python  
# ^
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304