0

in a file called test.py I have the following code

#!/usr/bin/python3

with open('test.txt','w') as File:
    print(1,file=File)

When I do

$ python3 test.py

It runs fine. But when I do

$ chmod +x test.py
$ ./test.py

I get a SyntaxError:

  File "./test.py", line 4
    print(1,file=File)
                ^
SyntaxError: invalid syntax

Any one has any idea why? Thank you

I'm running Python 3.3.1 in Ubuntu 13.04

  • What does `which python3` tell you is the path? – Martijn Pieters Apr 30 '13 at 16:34
  • 3
    The syntax error is thrown by Python 2.x; `/usr/bin/python3` is not what you think it is. – Martijn Pieters Apr 30 '13 at 16:35
  • `which python` is `/usr/bin/python3` which is python 3.3.1 (if I run `/usr/bin/python3` I get the Python 3.3.1 interpreter. More so, If I do `/usr/bin/python3 test.py` it all works nice) – Patricio Clark Apr 30 '13 at 16:40
  • 1
    @PatricioClark Very strange. What happens if you change the script to do: `import sys; print(sys.executable)`? Edit: might also be worth adding `print(sys.version)`. – Aya Apr 30 '13 at 16:45
  • Is it possible that `test.py` isn't what you think it is? A file open in the wrong editor for example? – Brendan Long Apr 30 '13 at 16:47
  • @Aya Oh, it prints `/usr/bin/python`, which is Python 2.7. So the thing is, why isn't it respecting the shabang? – Patricio Clark Apr 30 '13 at 16:49
  • @BrendanLong No, I actually had the problem in another (and bigger) script. I created this example specifically to isolate it. – Patricio Clark Apr 30 '13 at 16:51
  • Maybe there's whitespace at the beginning of the file? – Brendan Long Apr 30 '13 at 16:51
  • But you do run this exact script and have the same problem, right? – Brendan Long Apr 30 '13 at 16:51
  • @PatricioClark I've tried running the same script on Ubuntu 12.10, and I can't reproduce the problem. Try `xxd test.py` and check the first two bytes are 0x23 0x21. – Aya Apr 30 '13 at 16:52
  • @BrendanLong of test.py? The first character of the first line of the file is the # from the shabang. – Patricio Clark Apr 30 '13 at 16:52
  • Maybe the file starts with a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark), which your editor would probably hide. You could change the encoding in your editor to ASCII when you save, or use one of the methods [on this question](http://stackoverflow.com/questions/1068650/using-awk-to-remove-the-byte-order-mark) to remove it. – Brendan Long Apr 30 '13 at 16:53
  • @Aya the first line of the `xxd test.py` output is `0000000: 2321 2f75 7372 2f62 696e 2f65 6e76 2070 #!/usr/bin/env p` So the first two bytes are correct? – Patricio Clark Apr 30 '13 at 16:57
  • @PatricioClark ...and the second line? – Aya Apr 30 '13 at 16:58
  • @Aya The second line `0000010: 7974 686f 6e33 0a0a 7769 7468 206f 7065 ython3..with ope` – Patricio Clark Apr 30 '13 at 17:01
  • @PatricioClark Okay. There's nothing wrong with that, so it must be something with the shell you're using. Does it work if you do `bash -c "./test.py"`? – Aya Apr 30 '13 at 17:04
  • @BrendanLong I checked and no BOM at the beginning of the file – Patricio Clark Apr 30 '13 at 17:05
  • 1
    @Aya Thank you! I was using a zsh suffix alias that run python on *.py files. After removing that suffix alias everything is working perfectly. Big thank you to all of you. – Patricio Clark Apr 30 '13 at 17:12

1 Answers1

2

It looks like you're using a Python 2 interpreter for some reason. Maybe trying using env to search for the correct python3:

#!/usr/bin/env python3

with open('test.txt','w') as File:
    print(1,file=File)

env should find the same python3 that your shell does.

It's worth mentioning that this is a good idea even if you're not having problems, since it makes your scripts more portable by letting them run on any platform where python3 is available, no matter where it is.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188