Shebang not working

Hi all,

I've run into a problem I can't seem to solve. My simple code is as follows :-

# This is a simple listener for TCP/IP connections

#!/usr/bin python3
# -*- coding: utf-8 -*-

import socket
sock = socket.socket()
sock.settimeout(10)
print('Socket created and set timeout to 10')
port = 12345
sock.bind((socket.gethostname()), port)
sock.liste(2)
flag=True

while flag is True:
    try:
        count, addr = sock.accept()
        print('Incoming connection from ', addr)
        count.close()
        flag=False
    except socket.timeout as e:
        print('Socket timed out. Closing')
        flag=False

The problem is that I can't run the program using ./programname.py. It throws "import: command not found". I need to do python3 programname.py for it to work.

I wrote out the script entirely in vim on a raspberry pi. As far as I can tell its UTF-8 encoding. The environment is correct as well. I am not using a virtual environment for this simple test.

Anybody can shed some light on this? Thanks!

4 comments · 67% upvoted · other discussions

comments

[deleted]

Your shebang must be the first line in the file, and the file must be executable.

And in python3 source files are assumed to be UTF-8, so that line isn't needed.

EndUsersarePITA [OP]

Omg, it works. THANK YOU! I feel dumb

[deleted]

The recommended shebang for python3 is #!/usr/bin/env python3 as explained here.

EndUsersarePITA [OP]

Ah! Ok got it. I understand.