104👍
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
- [Django]-Catching DoesNotExist exception in a custom manager in Django
- [Django]-How to create a Django queryset filter comparing two date fields in the same model
- [Django]-Django – Reverse for '' not found. '' is not a valid view function or pattern name
24👍
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:
yum install centos-release-SCL
yum install python33
scl enable python33
Check this page out for more info on SCLs
- [Django]-Celery parallel distributed task with multiprocessing
- [Django]-Add a custom button to a Django application's admin page
- [Django]-Remove Labels in a Django Crispy Forms
17👍
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
Update: Change if condition to support higher versions if (3, 0) <= sys.version_info:
.
- [Django]-Error: No module named staticfiles
- [Django]-Django admin make a field read-only when modifying obj but required when adding new obj
- [Django]-Setting DEBUG = False causes 500 Error
13👍
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)
- [Django]-How to use regex in django query
- [Django]-Does SQLAlchemy have an equivalent of Django's get_or_create?
- [Django]-Using django-rest-interface
5👍
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
- [Django]-Django Forms and Bootstrap – CSS classes and <divs>
- [Django]-Get the list of checkbox post in django views
- [Django]-Django Setup Default Logging
- [Django]-How to import csv data into django models
- [Django]-Getting a count of objects in a queryset in Django
- [Django]-Why don't my south migrations work?
1👍
The urlparse in Python 2.7.11 was renamed to urllib.parse in Python 3.
So, if you have a code such from urlparse import urlparse, I suggest you change it to from urllib.parse import urlparse
Since you are using python 2.7.5, using the below will solve your poblem
from urlparse import urlparse
Instead of from urllib.parse import urlparse
- [Django]-UUID as default value in Django model
- [Django]-Django+Postgres: "current transaction is aborted, commands ignored until end of transaction block"
- [Django]-Check if celery beat is up and running
1👍
according to:
https://docs.python.org/3/library/urllib.parse.html
python 2x
from urlparse import urlparse
python 3x
from urllib.parse import urlparse
- [Django]-Get path of virtual environment in pipenv
- [Django]-Django-celery: No result backend configured
- [Django]-Return the current user with Django Rest Framework
0👍
Manually include urllib.parse: https://docs.python.org/3.3/library/urllib.parse.html#module-urllib.parse
- [Django]-How does one make logging color in Django/Google App Engine?
- [Django]-Rendering a template variable as HTML
- [Django]-In django do models have a default timestamp field?
-2👍
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/
- [Django]-Include intermediary (through model) in responses in Django Rest Framework
- [Django]-Specifying limit and offset in Django QuerySet wont work
- [Django]-Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found
- [Django]-Unit testing with django-celery?
- [Django]-Additional field while serializing django rest framework
- [Django]-In Django – Model Inheritance – Does it allow you to override a parent model's attribute?