1

project a urls.py b views.py

The case is like this. I am not being able to import the function from views.py in urls.py. It shows the error attempted relative import beyond top-level package.

from django.contrib import admin

from django.urls import path

from ..b.views import function


urlpatterns = [
    path('admin/', admin.site.urls),
    path('function/', function),]

from ..b.views import function ValueError: attempted relative import beyond top-level package

  • 1
    Please import this as `from b.views import function`. Relative imports are usually not a very good idea (especially when you do a lot of "walking") – Willem Van Onsem Aug 14 '19 at 09:50

3 Answers3

1

You can use

from b.views import function
Atom Store
  • 961
  • 1
  • 11
  • 35
0

In django, it is not recommended to use relative imports. You should import all files as they are present from the root of the django project. Example, for your case it should be:

from b import views
Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69
-1

Ensure that the to-be-imported module or a reference is in the path of your python program. To include higher ordered dictionaries TRY:

import sys
sys.path.append("..") # Adds higher directory to python modules path.

You might find your answer here StackOverflow: ValueError: attempted relative import beyond top-level package

edit: other comments suggest that you should not use relative imports.

cptnJ
  • 230
  • 2
  • 8