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),
]
Related
I have 3 apps inside django project (leadmanager) leads, frontend, accounts
Everything is working fine if I dont include accounts.urls (from accounts app) in leadmanager.urls but as soon as I include accounts.urls I getting the following error (all of these apps are registered in settings.py):
$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\urls\resolvers.py", line 591, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Danial Ahmed\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\Danial Ahmed\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\management\base.py", line 396, in check
databases=databases,
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\checks\registry.py", line 70,
in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf 'leadmanager.urls' does not appear to have any patterns in
it. If you see valid patterns in the file then the issue is probably caused by a circular import.
leadmanager.url (main):
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('frontend.urls')),
path('',include('leads.urls')),
path('',include('accounts.urls')),
]
lead.urls:
from rest_framework import routers
from .api import LeadViewSet
router = routers.DefaultRouter()
router.register('api/leads', LeadViewSet, 'leads')
app_name = 'leads'
urlpatterns = router.urls
accounts.urls:
from django.urls import path, include
from .api import ReigsterAPI
from knox import views as knox_views
app_name = 'accounts'
urlpatterns = [
path('api/auth', include('knox.urls')),
path('api/auth/register', RegisterAPI.as_view()),
]
frontend.urls:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index),
]
In .api I was import serializers.py but I had a typing mistake, my file was called serailizers.py, after fixing name of file everything started working!
I just wish that there was someway to detect this syntax errors, because the error I received was not very helpful.
I want to add a new app in the https://github.com/edx/edx-platform.
I am following this https://docs.djangoproject.com/en/2.2/intro/tutorial01/ document
/edx/app/edxapp/edx-platform/myapp/url.py
from django.conf.urls import url
from . import views
urlpatterns = [
url('', views.hello, name = 'hello'),
]
/edx/app/edxapp/edx-platform/lms/envs/common.py
INSTALLED_APPS = [
# Standard ones that are always installed...
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.humanize',
'django.contrib.messages',
'django.contrib.redirects',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.staticfiles',
'djcelery',
'myapp',
.............
/edx/app/edxapp/edx-platform/lms/urls.py
urlpatterns = [
url(r'^$', branding_views.index, name='root'), # Main marketing page, or redirect to courseware
url(r'', include('student.urls')),
# TODO: Move lms specific student views out of common code
url(r'^dashboard/?$', student_views.student_dashboard, name='dashboard'),
url(r'^change_enrollment$', student_views.change_enrollment, name='change_enrollment'),
url(r'^myapp/', include('myapp.urls')),
My Error
Traceback (most recent call last):
File "manage.py", line 123, in <module>
execute_from_command_line([sys.argv[0]] + django_args)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/management/base.py", line 327, in execute
self.check()
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 256, in check
for pattern in self.url_patterns:
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 407, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-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 "/edx/app/edxapp/edx-platform/lms/urls.py", line 6, in <module>
from django.conf.urls import include, url, path
ImportError: cannot import name path
Where I am going wrong. please let me know.
File "/edx/app/edxapp/edx-platform/lms/urls.py", line 6, in <module>
from django.conf.urls import include, url, path
ImportError: cannot import name path
The above error is saying that you can't import path from django.conf.urls. It seems you are using older version of django. so, try the following command to install old version.
$ pip install Django==1.11
and go to file /edx/app/edxapp/edx-platform/lms/urls.py and remove path from import. Then it looks like below.
from django.conf.urls import include, url
When i run 'python manage.py migrate' ,,it gives me this error ::
raise TypeError('view must be a callable or a list/tuple in the case of include().') TypeError: view must be a callable or a list/tuple in the case of include().
THE WHOLE TRACEBACK IS:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/prashant/Desktop/po/local/lib/python2.7/site-
packages/django/core/management/__init__.py", line 364, in
execute_from_command_line
utility.execute()
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/core/management/base.py", line 327, in execute
self.check()
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 62, in _run_checks
issues.extend(super(Command, self)._run_checks(**kwargs))
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 398, 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/prashant/Desktop/po/cope/urls.py", line 10, in <module>
url(r'^', include('opac.urls', namespace="opac")),
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/prashant/Desktop/po/opac/urls.py", line 5, in <module>
url(r'^$', 'forward'),
File "/home/prashant/Desktop/po/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 85, in url
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
opac/urls.py ::
from django.conf.urls import *
urlpatterns = ['opac.views',
url(r'^$', 'forward'),
url(r'^search/$', 'index'),
url(r'^search/book/(?P<book_id>\d+)', 'get_book'),
]
cope/urls.py ::
from django.conf.urls import *
from cope import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('opac.urls', namespace="opac")),
]
from django.conf.urls import *
from .views import forward,index,get_book
urlpatterns = [
url(r'^$', forward),
url(r'^search/$', index),
url(r'^search/book/(?P<book_id>\d+)', get_book),
]
use this format in your opac.urls file
I'm trying to add ChartsJS as an extra view in my Django App but I keep getting an Import Error. I have no idea why. I don't have any circular dependencies as far as I can find. Similar StackOverflow questions don't seem to match the problem. I'm totally stuck.
The problematic view is Analytics view.
project/app/urls.py:
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
from django.conf.urls import url
from future import standard_library
from survey.views import ConfirmView, IndexView, SurveyCompleted, SurveyDetail, AnalyticsView
from survey.views.survey_result import serve_result_csv
standard_library.install_aliases()
urlpatterns = [
url(r'^$', IndexView.as_view(), name='survey-list'),
url(r'^(?P<id>\d+)/', SurveyDetail.as_view(), name='survey-detail'),
url(r'^csv/(?P<pk>\d+)/', serve_result_csv, name='survey-result'),
url(r'^(?P<id>\d+)/completed/', SurveyCompleted.as_view(),
name='survey-completed'),
url(r'^(?P<id>\d+)-(?P<step>\d+)/', SurveyDetail.as_view(),
name='survey-detail-step'),
url(r'^confirm/(?P<uuid>\w+)/', ConfirmView.as_view(),
name='survey-confirmation'),
url(r'^analytics/$', AnalyticsView.as_view(),
name='analytics-view'),
]
project/app/views/analytics_view.py:
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
from builtins import super
from future import standard_library
from django.views.generic.base import TemplateView
from django.contrib.auth.models import User
standard_library.install_aliases()
import arrow
class AnalyticsView(TemplateView):
template_name = "survey/analytics.html"
...
I've read it may be to do with Settings, but I can't see any configuration problems there -
project/settings.py:
ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/bootstrap_admin/static/'
STATICFILES_DIRS = [
os.path.normpath(os.path.join(ROOT, '..', "survey", "static")),
]
Traceback:
Unhandled exception in thread started by <function wrapper at 0x05CBB9F0>
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line 125, 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 254, 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 405, 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 398, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\ronaldg\Documents\csa-survey\urls.py", line 18, in <module>
url(r'^survey/', include('survey.urls')),
File "C:\Python27\lib\site-packages\django\conf\urls\__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\ronaldg\Documents\csa-survey\survey\urls.py", line 9, in <module>
from survey.views import ConfirmView, IndexView, SurveyCompleted, SurveyDetail, AnalyticsView
ImportError: cannot import name AnalyticsView
Any ideas what I'm doing wrong?
The code you posted shows that your AnalyticsView is in its own file; you need to import that file.
from survey.views.analytics_view import AnalyticsView
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.