Script Can't Find Python3

Viewed 495

I'm trying to learn a bit of Python3, so, first things first: Hello World. But as simple as it is, I'm having a terrible time with line 1, the Shebang reference. I create the following script on my laptop running Mac Os 10.15.3

#!/usr/bin/env python3
print('Hello World')

I save it as python_test-01.py, make the script executable, and try to run it

Emonda:Scripts paul$ chmod 755 python_test-01.py 
Emonda:Scripts paul$ ls -l

-rwxr-xr-x@ 1 paul  staff   47 Mar  5 13:07 python_test-01.py

Emonda:Scripts paul$ ./python_test-01.py 
./python_test-01.py: line 1: #!/usr/bin/env: No such file or directory
./python_test-01.py: line 2: syntax error near unexpected token `'Hello World''
./python_test-01.py: line 2: `print('Hello World')'

Hmm. Can't find env. OK, I'll use a direct path in the shebang line instead of env. I edit the script to read

#!/usr/bin/python3
print('Hello World')

and run it

Emonda:Scripts paul$ ./python_test-01.py 
./python_test-01.py: line 1: #!/usr/bin/python3: No such file or directory
./python_test-01.py: line 2: syntax error near unexpected token `'Hello World''
./python_test-01.py: line 2: `print('Hello World')'

Now it can't find Python3 either. So I look around to make sure I actually have env and python3 installed and where they are.

Emonda:Scripts paul$ cd /usr/bin/
Emonda:bin paul$ pwd
/usr/bin
Emonda:bin paul$ whereis env
/usr/bin/env
Emonda:bin paul$ whereis python3
/usr/bin/python3

Yep, there they are, right where I said they would be in the Shebang line. /usr/bin/

A Second Chapter

After kicking this over again and again, I decide to try something different. I ssh over to my Raspberry Pi running Debian GNU/Linux. I note that it too has a version of Python3 installed in its /usr/bin/ directory. I use vim to write the same two line script starting with the same shebang: #!/usr/bin/python3 . Chmod 755. "Hello World". It works the first try.

What is so different between these two systems? Everything worked as expected in the Raspberry Pi. What's up with the Mac? Where should I be looking on the mac to find the reason the interpreter can't find Python3?

Thanks aging for hanging in there with me, Paul

1 Answers

After all this and all of your great input, it turns out that the script file on the MacOs system was saved UTF-8 with BOM. That was enough to make first line of the script, the Shebang, illegible to the interpreter. After saving the file the the UTF-8 no BOM encoding, it worked as expected.

Related