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

Go To StackoverFlow.com

30

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.

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):

Output:

ImportError at /stamp/
No module named urllib.parse
2015-03-31 00:03
by javiercruzweb


55

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

try:
    from urllib.parse import urlparse
except ImportError:
     from urlparse import urlparse
2016-05-30 12:49
by Agnaldo Marinho
Answer taking care about both py2 and py3 is much better :- - erm3nda 2017-05-27 09:12


17

You want urlparse using python2:

from urlparse import urlparse
2015-03-31 00:15
by Padraic Cunningham


15

With the information you have provided, your best bet will be to use 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 should change the import to:

from urlparse import urlparse

urllib was split into urllib.parse, urllib.request, and urllib.error in Python 3.

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

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

Check this page out for more info on SCLs

2015-03-31 00:28
by Minn Soe


6

python3 supports urllib.parse and python2 supports 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
2018-03-17 12:38
by Ram Idavalapati


5

Install six, the Python 2 and 3 Compatibility Library:

$ sudo -H pip install six

Use it:

from six.moves.urllib.parse import urlparse

(edit: I deleted the other answer)

2017-10-10 10:44
by Martin Thoma
lol, q: "No module named urllib.parse" a: install third party lib to do try..except for you xD . worst answer eve - Reishin 2018-03-29 11:13
IMO, this is the right answer. Six is an incredibly useful module for writing python2/3 compatible code. You can have four lines and an ugly try/catch, or you can just use six - Dan R 2018-04-13 18:04
@Reishin Six is the sixt most downloaded Python package in 2015 - only setuptools, requests, virtualenv, distribute and boto are downloaded more often (see my analysis). Chances are good that an experienced Python developer will have this already installed - Martin Thoma 2018-04-14 07:58
it is most downloaded as 90% of "developers" blindly copy-paste recipes from here with no knowledge about best practices. Well, at the end it is "dev" choice to overcrowd his product with 3d party libs, where he use only one function, which can be easily done by native python bu it results in shitty application with thousands of dependencies and shitty optimization - Reishin 2018-04-15 06:46
@DanR or you can wrap this try..catch in a function and use it....what a hell; or import it in init with as directive, and then re-import it from that package. But no, you need to use tons of code and dependencies for just nicely looking impor - Reishin 2018-04-15 06:47
@Reishin - Anytime you're developing software, you have to weigh your decisions when adding dependencies. There's risk in doing everything in Native Python because it creates more code that you have to manage, debug, unit test, etc. There's risk in bringing in modules because you're trusting the maintainers of the other package to properly maintain them. And odds are that if you're trying to write code that is python2/3 compatible, you will be using more than just this from six. I use it in many places - Dan R 2018-04-16 14:32


3

For python 3 pip install urllib

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

change the import urlparse to

from urllib import parse as urlparse
2017-03-28 05:51
by user482963


1

For Python 3, use the following:

import urllib.parse
2016-12-01 20:57
by Kingz


0

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!

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

2015-03-31 16:14
by javiercruzweb


0

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

2016-02-27 22:27
by Noel


-6

pip install -U websocket 

I just use this to fix my problem

2015-07-29 10:21
by user5168920
I've see that answer somewhere, and even if that does solve the problem because has urlparse as dependancy, is not the proper way to solve it - erm3nda 2017-05-27 09:11
Ads