./xx.py: line 1: import: command not found

后端 未结 6 1344
不知归路
不知归路 2020-12-03 13:32

I am trying to use this Python urllib2 Basic Auth Problem bit of code to download a webpage content from an URL which requires authentication. The code I am trying is:

6条回答
  •  萌比男神i
    2020-12-03 14:19

    If you run a script directly e.g., ./xx.py and your script has no shebang such as #!/usr/bin/env python at the very top then your shell may execute it as a shell script. POSIX says:

    If the execl() function fails due to an error equivalent to the [ENOEXEC] error defined in the System Interfaces volume of POSIX.1-2008, the shell shall execute a command equivalent to having a shell invoked with the pathname resulting from the search as its first operand, with any remaining arguments passed to the new shell, except that the value of "$0" in the new shell may be set to the command name. If the executable file is not a text file, the shell may bypass this command execution. In this case, it shall write an error message, and shall return an exit status of 126.

    Note: you may get ENOEXEC if your text file has no shebang.

    Without the shebang, you shell tries to run your Python script as a shell script that leads to the error: import: command not found.

    Also, if you run your script as python xx.py then you do not need the shebang. You don't even need it to be executable (+x). Your script is interpreted by python in this case.

    On Windows, shebang is not used unless pylauncher is installed. It is included in Python 3.3+.

提交回复
热议问题