无法在 Python 中导入 urllib

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

Cannot import urllib in Python

pythonpython-2.7

提问by Square9627

I would like to import urllibto use the function 'request'. However, I encountered an error when trying to download via Pycharm:

我想导入urllib以使用函数“ request”。但是,我在尝试通过 Pycharm 下载时遇到错误:

"Could not find a version that satisfies the requirement urllib (from versions: ) No matching distribution found for urllib"

“找不到满足 urllib 要求的版本(来自版本:)没有找到与 urllib 匹配的发行版”

I tried pip install urllibbut still had the same error. I am using Python 2.7.11. Really appreciate any help

我试过了,pip install urllib但仍然有同样的错误。我正在使用 Python 2.7.11。真的很感谢任何帮助

回答by elethan

A few things:

一些东西:

  1. As metioned in the comments, urllibis not installed through pip, it is part of the standard library, so you can just do import urllibwithout installation.
  2. Python 3.x has a urllib.requestmodule, but Python 2.x does not, as far as I know.
  3. The functionality that you are looking for from urllib.requestis most likely contained in urllib2(which is also part of the standard library), but you might be even better off using requests, which probably does need to be installed through pipin your case:

    pip install requests
    

    In fact, the urllib2documentation itself recommends that you use the requestslibrary "for a higher-level HTTP client interface" - but I am not sure what you wish to do, so it is hard to say what would be best for your particular use case.

  1. 正如评论中提到的,urllib不是通过 安装的pip,它是标准库的一部分,所以你可以import urllib不用安装。
  2. urllib.request据我所知,Python 3.x 有一个模块,但 Python 2.x 没有。
  3. 您正在寻找的功能urllib.request很可能包含在urllib2(它也是标准库的一部分)中,但您最好使用requestspip在您的情况下可能需要安装它:

    pip install requests
    

    事实上,urllib2文档本身建议您使用该requests库“用于更高级别的 HTTP 客户端接口” - 但我不确定您希望做什么,因此很难说哪种方式最适合您的特定用例。