在 Python3.6 中安装 urllib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47730259/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:20:00  来源:igfitidea点击:

installing urllib in Python3.6

pythonpython-3.xurllib

提问by Mohamed Mahdi

I would like to import urllib to use the function 'request'. However, I encountered an error when trying to do so. I tried pip install urllib but still had the same error. I am using Python 3.6. Really appreciate any help.

我想导入 urllib 以使用函数“请求”。但是,我在尝试这样做时遇到了错误。我试过 pip install urllib 但仍然有同样的错误。我正在使用 Python 3.6。真的很感激任何帮助。

i do import urllib.request using this code:

我确实使用以下代码导入 urllib.request:

import urllib.request, urllib.parse, urllib.error 
fhand = urllib.request.urlopen('data.pr4e.org/romeo.txt') 
counts = dict()
for line in fhand: 
    words = line.decode().split() 
for word in words: 
    counts[word] = counts.get(word, 0) + 1 
print(counts) 

but it gives me this error: ModuleNotFoundError: No Module named 'urllib.parse'; 'urllib' is not a package

但它给了我这个错误: ModuleNotFoundError: No Module named 'urllib.parse'; 'urllib' 不是一个包

here is a screenshot for the error

这是错误的屏幕截图

回答by Xero Smith

urllibis a standard library, you do not have to install it. Simply import urllib

urllib是一个标准库,您不必安装它。简单地import urllib

回答by Kardi Teknomo

The corrected code is

更正后的代码是

import urllib.request
fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
counts = dict()
for line in fhand: 
    words = line.decode().split() 
for word in words: 
    counts[word] = counts.get(word, 0) + 1 
print(counts) 

running the code above produces

运行上面的代码会产生

{'Who': 1, 'is': 1, 'already': 1, 'sick': 1, 'and': 1, 'pale': 1, 'with': 1, 'grief': 1}

回答by Ahmad Nourallah

urllibis a standard python library (built-in) so you don't have to install it. just import it if you need to use requestby:

urllib是一个标准的 python 库(内置),所以你不必安装它。如果您需要request通过以下方式使用,只需导入它:

import urllib.request

if it's not work maybe you compiled python in wrong way, so be kind and give us more details.

如果它不起作用,也许你以错误的方式编译了 python,所以请善待并提供更多细节。

回答by Akshat Divya

This happens because your local module named urllib.pyshadows the installed requests module you are trying to use. The current directory is preapended to sys.path, so the local name takes precedence over the installed name.

发生这种情况是因为您的本地模块urllib.py隐藏了您尝试使用的已安装请求模块。当前目录预先附加到 sys.path,因此本地名称优先于安装名称。

An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import.

出现这种情况时的一个额外调试提示是仔细查看回溯,并意识到有问题的脚本名称与您尝试导入的模块匹配。

Rename your file to something else like url.py. Then It is working fine. Hope it helps!

将您的文件重命名为其他类似url.py. 然后它工作正常。希望能帮助到你!