Reputation: 6570
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
Upvotes: 1
Views: 2640
Reputation: 20344
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.
Upvotes: 2
Reputation: 3
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
Upvotes: 0
Reputation: 32444
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.
Upvotes: 0
Reputation: 11
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.
Upvotes: -1
Reputation: 8159
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 :-).
Upvotes: 0