Python 没有名为 urllib.parse 的模块(我应该如何安装它?)

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

no module named urllib.parse (How should I install it?)

pythondjangourllib

提问by javiercruzweb

I'm trying to run a REST API on CentOS 7, I read urllib.parse is in Python 3 but I'm using Python 2.7.5 so I don't know how to install this module.

我正在尝试在 CentOS 7 上运行 REST API,我读到 urllib.parse 在 Python 3 中,但我使用的是 Python 2.7.5,所以我不知道如何安装此模块。

I installed all the requirements but still can't run the project.

我安装了所有要求,但仍然无法运行该项目。

When I'm looking for a URL I get this (I'm using the browsable interface):

当我在寻找一个 URL 时,我得到了这个(我正在使用可浏览的界面):

Output:

输出:

ImportError at /stamp/
No module named urllib.parse

回答by Padraic Cunningham

You want urlparse using python2:

你想要使用 python2 的 urlparse:

from urlparse import urlparse

回答by Minn Soe

With the information you have provided, your best bet will be to use Python 3.x.

根据您提供的信息,最好的选择是使用 Python 3.x。

Your error suggests that the code may have been written for Python 3 given that it is trying to import urllib.parse. If you've written the software and have control over its source code, you shouldchange the import to:

您的错误表明代码可能是为 Python 3 编写的,因为它正在尝试导入urllib.parse. 如果您已经编写了软件并可以控制其源代码,则应将导入更改为:

from urlparse import urlparse

urllibwas split into urllib.parse, urllib.request, and urllib.errorin Python 3.

urllib在 Python 3 中被拆分为urllib.parseurllib.requesturllib.error

I suggest that you take a quick look at software collections in CentOS if you are not able to changethe imports for some reason. You can bring in Python 3.3 like this:

如果由于某种原因无法更改导入,我建议您快速查看 CentOS 中的软件集合。您可以像这样引入 Python 3.3:

  1. yum install centos--release--SCL
  2. yum install python33
  3. scl enable python33
  1. yum install centos--release--SCL
  2. yum install python33
  3. scl enable python33

Check thispage out for more info on SCLs

查看页面以获取有关 SCL 的更多信息

回答by javiercruzweb

The problem was because I had a lower version of Django (1.4.10), so Django Rest Framework need at least Django 1.4.11 or bigger. Thanks for their answers guys!

问题是因为我有一个较低版本的 Django (1.4.10),所以 Django Rest Framework 至少需要 Django 1.4.11 或更高版本。感谢他们的回答伙计们!

Here the link for the requirements of Django Rest: http://www.django-rest-framework.org/

这里是 Django Rest 要求的链接:http: //www.django-rest-framework.org/

回答by user5168920

pip install -U websocket 

I just use this to fix my problem

我只是用它来解决我的问题

回答by Noel

Manually include urllib.parse: https://docs.python.org/3.3/library/urllib.parse.html#module-urllib.parse

手动包含 urllib.parse:https://docs.python.org/3.3/library/urllib.parse.html#module-urllib.parse

回答by Agnaldo Marinho

If you need to write code which is Python2 and Python3 compatible you can use the following import

如果您需要编写与 Python2 和 Python3 兼容的代码,您可以使用以下导入

try:
    from urllib.parse import urlparse
except ImportError:
     from urlparse import urlparse

回答by Kingz

For Python 3, use the following:

对于 Python 3,请使用以下内容:

import urllib.parse

回答by user482963

For python 3 pip install urllib

对于 python 3 pip install urllib

find the utils.pyin %PYTHON_HOME%\Lib\site-packages\solrcloudpy\utils.py

发现utils.py在%PYTHON_HOME%\ LIB \站点包\ solrcloudpy \ utils.py

change the import urlparseto

更改import urlparse

from urllib import parse as urlparse

回答by Martin Thoma

Install six, the Python 2 and 3 Compatibility Library:

安装、Python 2 和 3 兼容库:

$ sudo -H pip install six

Use it:

用它:

from six.moves.urllib.parse import urlparse

(edit: I deleted the other answer)

(编辑:我删除了另一个答案)

回答by Ram Idavalapati

python3supports urllib.parseand python2supports urlparse

python3支持urllib.parsepython2支持urlparse

If you want both compatible then the following code can help.

如果您希望两者兼容,那么以下代码可以提供帮助。

import sys

if ((3, 0) <= sys.version_info <= (3, 9)):
    from urllib.parse import urlparse
elif ((2, 0) <= sys.version_info <= (2, 9)):
    from urlparse import urlparse

Update: Change if condition to support higher versions if (3, 0) <= sys.version_info:.

更新:更改条件以支持更高版本if (3, 0) <= sys.version_info: