Run script in python shell with < gives error but with exec() doesn't

When I run the command: “python shell <” I get a an error message “NameError: name ‘socket’ is not defined” and when I use exec function to run the inside “python shell” I don’t have any errors. Can someone explain me what’s the problem with the first command?
import socket

def allowed_gai_family():
return socket.AF_INET


I cannot recreate this behavior. This includes running it both under shell and shell_plus, and even trying variations like cat | python shell

At no time do I get the error you’re describing. The only thing I can think of is a non-printable character in there somewhere causing issues.

If you’re calling your script, it may shadow the standard library socket module. Call it something else like, as Ken used.

Good catch. Actually, I thought they were using ‘’