1

I'm looking some clues or solutions to fix below weird problem.

[sc-d02-087-017:~]$ cat mytest.py
#!/build/toolchain/lin64/python-3.5.1/bin/python
import platform, sys
print(platform.system())
sys.exit(0)

I'm trying to run my test program as shell executable, but shell not recognizing that its a python program that has to interpreted by given /build/toolchain/lin64/python-3.5.1/bin/python, instead throwing err.

[sc-d02-087-017:~]$ ./mytest.py
./mytest.py: line 2: import: command not found
./mytest.py: line 3: syntax error near unexpected token `platform.system'
./mytest.py: line 3: `print(platform.system())'

But this works as expected with python prompt and/or as command line

[sc-d02-087-017:~]$ /build/toolchain/lin64/python-3.5.1/bin/python -c 'import platform, sys;print(platform.system());'
Linux

my machine is 64 arch based

[sc-d02-087-017:~]$ uname -a
Linux sc-d02-087-017 2.6.18-308.8.1.el5 #1 SMP Tue May 29 14:57:25 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux

As requested in the comments:

[sc-d02-087-017:~]$ xxd mytest.py | head
0000000: 2321 2f62 7569 6c64 2f74 6f6f 6c63 6861 #!/build/toolcha
0000010: 696e 2f6c 696e 3634 2f70 7974 686f 6e2d in/lin64/python-
0000020: 332e 352e 312f 6269 6e2f 7079 7468 6f6e 3.5.1/bin/python
0000030: 332e 350a 696d 706f 7274 2070 6c61 7466 3.5.import platf
0000040: 6f72 6d2c 2073 7973 0a70 7269 6e74 2870 orm, sys.print(p
0000050: 6c61 7466 6f72 6d2e 7379 7374 656d 2829 latform.system()
0000060: 290a 7379 732e 6578 6974 2830 290a      ).sys.exit(0).

[sc-d02-087-017:~]$ ls -l mytest.py
-rwxr-xr-x 1 mmopuru mts 110 Jun 2 17:23 mytest.py
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Murali Mopuru
  • 6,086
  • 5
  • 33
  • 51
  • check `/build/toolchain/lin64/python-3.5.1/bin/python` – Karoly Horvath Jun 03 '16 at 00:14
  • checking what? it's existence ? it does, thats why prompt works. – Murali Mopuru Jun 03 '16 at 00:16
  • [sc-d02-087-017:~]$ xxd mytest.py | head 0000000: 2321 2f62 7569 6c64 2f74 6f6f 6c63 6861 #!/build/toolcha 0000010: 696e 2f6c 696e 3634 2f70 7974 686f 6e2d in/lin64/python- 0000020: 332e 352e 312f 6269 6e2f 7079 7468 6f6e 3.5.1/bin/python 0000030: 332e 350a 696d 706f 7274 2070 6c61 7466 3.5.import platf 0000040: 6f72 6d2c 2073 7973 0a70 7269 6e74 2870 orm, sys.print(p 0000050: 6c61 7466 6f72 6d2e 7379 7374 656d 2829 latform.system() 0000060: 290a 7379 732e 6578 6974 2830 290a ).sys.exit(0). – Murali Mopuru Jun 03 '16 at 00:30
  • @MuraliMopuru: What is the output of `/build/toolchain/lin64/python-3.5.1/bin/python ./mytest.py`? – Bill Lynch Jun 03 '16 at 00:33
  • [sc-d02-087-017:~]$ /build/toolchain/lin64/python-3.5.1/bin/python mytest.py Linux – Murali Mopuru Jun 03 '16 at 00:34
  • What do you get from `file /build/toolchain/lin64/python-3.5.1/bin/python`? – Peter Brittain Jun 06 '16 at 06:51
  • 5
    I humbly submit that your `xxd` output does not match your `cat` output. In particular - an extra `3.5` appears in the `xxd` output. I believe your shebang line is incorrect and actually reads `#!/build/toolchain/lin64/python-3.5.1/bin/python3.5` which probably points to a file that does not exist. Therefore, your file is being interpreted as a shell script, hence the error that you see. If this turns out to be the case, let me know and I'll post a proper answer for the bounty :) (cc @billlynch) – J Richard Snape Jun 06 '16 at 11:04
  • Nice spot @JRichardSnape! I looked at exactly that possibility, but despite checking twice, couldn't see it... Damnit! :-) – Peter Brittain Jun 06 '16 at 13:34
  • Does the command `namei /build/toolchain/lin64/python-3.5.1/bin/python3.5` reveal anything interesting? – Robᵩ Jun 06 '16 at 18:09
  • @Richard, plz post your answer to accept – Murali Mopuru Jun 08 '16 at 05:12

5 Answers5

2

As you are aware, the error message is because your python script is being interpreted directly as a shell script.

Usually this happens when you have no "shebang" line (!#/path/to/python/executable). As this does not apply, I looked a bit harder and noticed that your xxd output does not match your cat output. In particular - an extra 3.5 appears in the xxd output. I believe your shebang line is incorrect and actually reads #!/build/toolchain/lin64/python-3.5.1/bin/python3.5 which probably points to a file that is not the one you thought / does not exist. Therefore, your file is being interpreted as a shell script, hence the error that you see.

However I would expect a non-existent file to give an error like:

bad interpreter: No such file or directory

therefore I wonder if your error is more subtle than that, in that your #! line is pointing somewhere other that you think, but that it still points to a valid executable.

N.B. A test you could do, but which does not appear in your question is to run

/build/toolchain/lin64/python-3.5.1/bin/python ./mytest.py

If that works the same as

/build/toolchain/lin64/python-3.5.1/bin/python -c 'import platform, sys;print(platform.system());'

then you almost certainly have a problem/typo in your #! line.

J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
0

I get a wide range of similar issues by saving the python 3 file with different encodings from UTF8 (e.g. UTF16), so I suspect that's your problem. Try making sure it is saved with a "normal" encoding (UTF8) and perhaps that will fix it!

If it does, I suspect that the "problem" is that python3 has much better support for differently encoded files... bash does not it seems :-).

daphtdazz
  • 7,754
  • 34
  • 54
0

Your shebang line may be too long for your system. Try shortening it. For example, create a symlink to /build/toolchain/lin64/python-3.5.1:

ln -s /build/toolchain/lin64/python-3.5.1 /tmp/xyz

and change the shebang line to #!/tmp/xyz/bin/python. If that fixes the problem, then you can choose a more proper short way of referring to your build of python.

Community
  • 1
  • 1
Leon
  • 31,443
  • 4
  • 72
  • 97
0

try to add that path to you profile /build/toolchain/lin64/python-3.5.1/bin/python & execution path file either .bash_profile or depening on the shell you use, then switch user to the same login or execute the profile like . ./.bash_profile and try agin with your script....hope it helps

prakasham
  • 3
  • 1
-1

I have no idea why you used ./.

Execute Python script like this:

python mytest.py        # and then just hit enter.

If that doesn't work then just let me know.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Bappy
  • 11
  • 1
  • 1
    The OP wants to know why ./ doesn't work. He has specified the interpreter on the first line with the #! so the script should run the program automatically in python when called using ./. If he uses python mytest.py this will explicitly call the python interpreter and the first line is redundant – Alan Kavanagh Jun 09 '16 at 12:19
  • your answer is right i do not know why they downvote it?!?!?! – yasin lachini May 29 '19 at 12:12