I am learning how to create a Rest API in django. in views.py, adding
from rest_framework.urlpatterns import format_suffix_patterns
is causing
ImportError: cannot import name importlib. I searched that this is because I am using Django 1.9 or more maybe. But can't figure out how to solve this.
Thanks for help.
Tutorial that I am following: https://www.youtube.com/watch?v=QW_5xCCPWFk&index=40&list=PL6gx4Cwl9DGBlmzzFcLgDhKTTfNLfX1IK
full traceback
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x7f1b5badc500>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/sonali/Videos/rest_api_project/rest_api_project/urls.py", line 20, in <module>
from rest_framework.urlpatterns import format_suffix_patterns
File "/usr/local/lib/python2.7/dist-packages/rest_framework/urlpatterns.py", line 4, in <module>
from rest_framework.settings import api_settings
File "/usr/local/lib/python2.7/dist-packages/rest_framework/settings.py", line 23, in <module>
from django.utils import importlib
ImportError: cannot import name importlib
Python version 2.7.6
The django.utils.importlib module was removed in Django 1.9. It looks as if you are running an old version of rest framework that does not support Django 1.9.
Support for Django 1.9 was added in rest framework 3.3, and the current version is 3.6.3.
If your tutorial was written for Django 1.8, then you might find it easier to complete the tutorial using Django 1.8. If it was written for an earlier version of Django that that, then it would probably be better to look for a new tutorial.
Related
This question already has answers here:
Unable to import path from django.urls
(15 answers)
Closed 2 years ago.
After starting python manage.py run server throws such an error, before creating the directory, the server page started correctly.
M:\Project_Django\mysite>python manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x043E18F0>
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line 124, in inner_run
self.check(display_num_errors=True)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Python27\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Python27\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "C:\Python27\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
return check_method()
File "C:\Python27\lib\site-packages\django\urls\resolvers.py", line 256, in check
for pattern in self.url_patterns:
File "C:\Python27\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python27\lib\site-packages\django\urls\resolvers.py", line 407, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python27\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python27\lib\site-packages\django\urls\resolvers.py", line 400, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "M:\Project_Django\mysite\mysite\urls.py", line 17, in <module>
from django.urls import path, include,strona
ImportError: cannot import name path
Last version to suport python 2.7 was django 1.11 which does not have new path syntax, look into proper documentation. also consider updating your Django version, if you are just starting use python 3.* instead
Hi could anyone help me fix 'ImportError: cannot import name url' problem?
I have followed tutorial here https://docs.djangoproject.com/en/1.9/intro/tutorial01/
I have tried another tutorial https://docs.djangoproject.com/zh-hans/2.0/ref/urls/#django.urls.include
but neither of them worked
My Django version is 1.11.20
Performing system checks...
Unhandled exception in thread started by Traceback
(most recent call last):
File
"/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py",
line 228, in wrapper fn(*args, **kwargs)
File
"/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py",
line 124, in inner_run self.check(display_num_errors=True)
File
"/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",
line 359, in check
include_deployment_checks=include_deployment_checks,
File
"/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",
line 346, in _run_checks return
checks.run_checks(**kwargs)
File
"/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py",
line 81, in run_checks new_errors =
check(app_configs=app_configs)
File
"/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py",
line 16, in check_url_config return
check_resolver(resolver)
File
"/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py",
line 26, in check_resolver return check_method()
File
"/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py",
line 256, in check for pattern in
self.url_patterns:
File
"/usr/local/lib/python2.7/dist-packages/django/utils/functional.py",
line 35, in get res =
instance.dict[self.name] = self.func(instance)
File
"/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py",
line 407, in url_patterns patterns =
getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File
"/usr/local/lib/python2.7/dist-packages/django/utils/functional.py",
line 35, in get res =
instance.dict[self.name] = self.func(instance)
File
"/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py",
line 400, in urlconf_module return
import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/init.py", line 37, in
import_module
import(name) File "/home/adduser/cantera_correction/mysite/urls.py", line 16, in
from
django.conf.urls import include, path
ImportError: cannot import name path
path was introduced in django since Django 2.0. So, if you are using Django 1.11, then you can't use it. You need to define urls like this:
from django.conf.urls import url, include
urlpatterns = [
# rest of the urls
url(r'^$', HomeView.as_view()),
]
correct your imports to this:
from django.urls import path, include
This code will work for you.
from django.urls import path
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
]
I'm upgrading from django 1.6.5 to django 1.9, and in the process upgrading several middleware classes. Some of those middleware classes use models during the process_request or process_response phases. However, I'm getting a AppRegistryNotReady: Apps aren't loaded yet. error attempting to use them.
Is there a way to import models during middleware?
Should I move my import statements into the process_request / process_response methods?
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/api/web_transaction.py", line 1329, in _nr_wsgi_application_wrapper_
result = wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/api/web_transaction.py", line 1329, in _nr_wsgi_application_wrapper_
result = wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 158, in __call__
self.load_middleware()
File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/common/object_wrapper.py", line 302, in _wrapper
result = wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 51, in load_middleware
mw_class = import_string(middleware_path)
File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 20, in import_string
module = import_module(module_path)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/web/MyJobs/MyJobs/apache/../middleware.py", line 9, in <module>
from django.contrib.sites.models import Site
File "/usr/local/lib/python2.7/dist-packages/django/contrib/sites/models.py", line 83, in <module>
class Site(models.Model):
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
AppRegistryNotReady: Apps aren't loaded yet.
You need to use the new API to get a WSGI handler:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This will call django.setup() for you, which will populate the app registry.
I have upgraded django version from 1.8 into 1.9 and django rest framework to 3.3.3. I am getting this exception:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I have tried as follows but exception is still there.
#__init__.py
default_app_config = 'panel.apps.PanelConfig'
And also
#apps.py
from django.apps import AppConfig
class PanelConfig(AppConfig):
name = 'panel'
def ready(self):
from panel import receivers
for all apps and added these to installed apps
'api.apps.ApiConfig',
'billing.apps.ApiConfig',
'incoming.apps.IncomingConfig',
'outgoing.apps.OutgoingConfig',
'panel.apps.PanelConfig',
This is my full traceback:
Unhandled exception in thread started by <function wrapper at 0x7f3eec09c7d0>
Traceback (most recent call last):
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/admin_tools/dashboard/__init__.py", line 1, in <module>
from admin_tools.dashboard.dashboards import *
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/admin_tools/dashboard/dashboards.py", line 13, in <module>
from django.contrib.contenttypes.models import ContentType
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 161, in <module>
class ContentType(models.Model):
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Exception is still there ? What is the problem ? I am not getting ?
The traceback shows you that the problem is occuring in admin_tools.
from admin_tools.dashboard.dashboards import *
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/admin_tools/dashboard/dashboards.py", line 13, in <module>
from django.contrib.contenttypes.models import ContentType
It looks like it has been fixed, so try upgrading to the latest release, currently 0.7.2.
E:\pythonwebproject\pythonweb>python manage.py runserver
Validating models...
Unhandled exception in thread started by <function wrapper at 0x01DE69F0>
Traceback (most recent call last):
File "C:\Python26\Lib\site-packages\django\utils\autoreload.py", line 93, in wrapper
fn(*args, **kwargs)
File "C:\Python26\Lib\site-packages\django\core\management\commands\runserver.py", line 92, in inner_r
self.validate(display_num_errors=True)
File "C:\Python26\Lib\site-packages\django\core\management\base.py", line 277, in validate
num_errors = get_validation_errors(s, app)
File "C:\Python26\Lib\site-packages\django\core\management\validation.py", line 35, in get_validation_
for (app_name, error) in get_app_errors().items():
File "C:\Python26\Lib\site-packages\django\db\models\loading.py", line 166, in get_app_errors
self._populate()
File "C:\Python26\Lib\site-packages\django\db\models\loading.py", line 72, in _populate
self.load_app(app_name, True)
File "C:\Python26\Lib\site-packages\django\db\models\loading.py", line 96, in load_app
models = import_module('.models', app_name)
File "C:\Python26\Lib\site-packages\django\utils\importlib.py", line 35, in import_module
__import__(name)
File "C:\Python26\Lib\site-packages\django\contrib\auth\models.py", line 21, in <module>
from django.contrib.contenttypes.models import ContentType
File "C:\Python26\Lib\site-packages\django\contrib\contenttypes\models.py", line 127, in <module>
class ContentType(models.Model):
File "C:\Python26\Lib\site-packages\django\db\models\base.py", line 88, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
TypeError: Error when calling the metaclass bases
__init__() keywords must be strings
This bug report is the only resource I can find with similar symptoms. Are you using django 1.5? Try upgrading your python install to 2.7 and see if that rectifies the issue.
You are using the development version of Django (soon to be 1.5) with Python 2.6. It only works with 2.7 and 3.3. You should either upgrade your Python version, or use the released Django version 1.4.