Python ./xx.py:第 1 行:导入:未找到命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22275350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
./xx.py: line 1: import: command not found
提问by user2481422
I am trying to use this Python urllib2 Basic Auth Problembit of code to download a webpage content from an URL which requires authentication. The code I am trying is:
我正在尝试使用此Python urllib2 Basic Auth Problem代码从需要身份验证的 URL 下载网页内容。我正在尝试的代码是:
import urllib2, base64
request = urllib2.Request("http://api.foursquare.com/v1/user")
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
It's showing me:
它向我展示:
./xx.py: line 1: import: command not found
./xx.py: line 3: syntax error near unexpected token `('
./xx.py: line 3: `request = urllib2.Request("http://api.foursquare.com/v1/user")'
I am wondering what I am doing wrong? I am using Python 2.7.5. How can I download file contents from an URL which requires authentication?
我想知道我做错了什么?我正在使用Python 2.7.5. 如何从需要身份验证的 URL 下载文件内容?
采纳答案by Sharif Mamun
It's not an issue related to authentication at the first step. Your importis not working. So, try writing this on first line:
这不是与第一步身份验证相关的问题。你import不工作。所以,试着在第一行写这个:
#!/usr/bin/python
and for the time being run using
并暂时使用
python xx.py
For you here is one explanation:
对你来说,这是一种解释:
>>> abc = "Hei Buddy"
>>> print "%s" %abc
Hei Buddy
>>>
>>> print "%s" %xyz
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print "%s" %xyz
NameError: name 'xyz' is not defined
At first, I initialized abc variable and it works fine. On the otherhand, xyz doesn't work as it is not initialized!
起初,我初始化了 abc 变量,它工作正常。另一方面, xyz 不起作用,因为它没有初始化!
回答by Alex Thornton
Are you using a UNIX based OS such as Linux? If so, add a shebangline to the very top of your script:
您是否使用基于 UNIX 的操作系统,例如 Linux?如果是这样,请在脚本的最顶部添加一条shebang行:
#!/usr/bin/python
Underneath which you would have the rest of the code (xx.pyin your case) that you already have. Then run that same command at the terminal:
在它下面,您将拥有您已经拥有的其余代码(xx.py在您的情况下)。然后在终端运行相同的命令:
$ python xx.py
This should then work fine, as it is now interpreting this as Python code. However when running from the terminal this does not matter as pythontells how to interpret it here. What it does allow you to do is execute it outside the terminal, i.e. executing it from a file browser.
这应该可以正常工作,因为它现在将其解释为 Python 代码。但是,当从终端运行时,这与python在此处解释如何解释无关。它确实允许您在终端之外执行它,即从文件浏览器执行它。
回答by jfs
If you run a script directly e.g., ./xx.pyand your script has no shebang such as #!/usr/bin/env pythonat the very top then your shell may execute it as a shell script. POSIX says:
例如,如果您直接运行脚本,./xx.py并且您的脚本#!/usr/bin/env python在最顶部没有shebang,那么您的shell 可能会将其作为shell 脚本执行。POSIX 说:
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.
如果 execl() 函数由于与 POSIX.1-2008 的系统接口卷中定义的 [ENOEXEC] 错误等效的错误而失败,则 shell 应执行一个命令,该命令等效于使用搜索产生的路径名调用 shell作为它的第一个操作数,将任何剩余的参数传递给新 shell,除了新 shell 中的“$0”值可以设置为命令名称。如果可执行文件不是文本文件,shell 可能会绕过此命令执行。在这种情况下,它将写入错误消息,并返回退出状态 126。
Note: you may get ENOEXECif your text file has no shebang.
注意:ENOEXEC如果您的文本文件没有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.
如果没有shebang,您的shell 会尝试将您的Python 脚本作为导致错误的shell 脚本运行:import: command not found.
Also, if you run your script as python xx.pythen you do not need the shebang. You don't even need it to be executable (+x). Your script is interpreted by pythonin this case.
此外,如果您按原样运行脚本,python xx.py则不需要 shebang。您甚至不需要它是可执行的 ( +x)。python在这种情况下,您的脚本被解释。
On Windows, shebang is not used unless pylauncher is installed. It is included in Python 3.3+.
在 Windows 上,除非安装了 pylauncher,否则不会使用 shebang 。它包含在 Python 3.3+ 中。
回答by Jeonghum
I've experienced the same problem and now I just found my solution to this issue.
我遇到了同样的问题,现在我刚刚找到了解决这个问题的方法。
#!/usr/bin/python
import sys
import os
os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))
This is the code[1] for my case. When I tried this script I received error message like :
这是我的情况的代码[1]。当我尝试这个脚本时,我收到了如下错误消息:
import: command not found
导入:找不到命令
I found people talks about the shebang. As you see there is the shebang in my python code above. I tried these and those trials but didn't find a good solution.
我发现人们在谈论shebang。如您所见,我上面的python 代码中有shebang。我尝试了这些和那些试验,但没有找到好的解决方案。
I finally tried to type the shebang my self.
我终于试着自己输入shebang。
#!/usr/bin/python
and removed the copied one.
并删除了复制的一个。
And my problem solved!!!
我的问题解决了!!!
I copied the code from the internet[1].
我从互联网上复制了代码[1]。
And I guess there had been some unseeable(?) unseen special characters in the original copied shebang statement.
而且我猜在原始复制的shebang语句中存在一些看不见的(?)看不见的特殊字符。
I use vim, sometimes I experience similar problems.. Especially when I copied some code snippet from the internet this kind of problems happen.. Web pages have some virus special characters!! I doubt. :-)
我用vim,有时也遇到类似的问题..特别是当我从网上复制一些代码片段时会发生这种问题..网页有一些病毒特殊字符!!我怀疑。:-)
Journeyer
旅人
PS) I copied the code in Windows 7 - host OS - into the Windows clipboard and pasted it into my vim in Ubuntu - guest OS. VM is Oracle Virtual Machine.
PS)我将 Windows 7 - 主机操作系统中的代码复制到 Windows 剪贴板中,然后将其粘贴到 Ubuntu - 来宾操作系统中的 vim 中。VM 是 Oracle 虚拟机。
[1] http://nathanhoad.net/how-to-meld-for-git-diffs-in-ubuntu-hardy
[1] http://nathanhoad.net/how-to-meld-for-git-diffs-in-ubuntu-hardy
回答by SJ26
回答by Li Li
When you see "import: command not found" on the very first import, it is caused by the parser not using the character encoding matching your py file. Especially when you are not using ASCII encoding in your py file.
当您在第一次导入时看到“import: command not found”时,这是由解析器未使用与您的 py 文件匹配的字符编码引起的。特别是当您没有在 py 文件中使用 ASCII 编码时。
The way to get it right is to specify the correct encoding on top of your py file to match your file character encoding.
正确的方法是在 py 文件顶部指定正确的编码以匹配文件字符编码。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os

