3
votes

I'm new to Django and am trying to create a very simple app off of a tutorial I found online.


Working on a mac
Django Version 2.0.7
Python 3.7.0

My file structure:
helloworld
......venv
..........(other files)
......helloworld_project
..........(other files)
......manage.py
......pages
.........._ pycache _
..............otherfiles
..........admin.py
..........apps.py
..........migrations
..............(other files)
..........models.py
..........tests.py
..........urls.py
..........views.py

The problem: when I run my urls.py file, I get the following message:

Traceback (most recent call last):
  File "/Users/Bethany/Desktop/helloworld/pages/urls.py",         
line 3, in <module>
    from pages import views
ModuleNotFoundError: No module named 'pages'

My urls.py file:

# pages/urls.py
from django.urls import path
from pages import views

urlpatterns = [
    path('', views.homePageView, name='home')
]

I've tried replacing "from pages import views" with "from . import views" and get the same message.

I've looked through a few similar questions on stack overflow, but haven't had success with finding a solution to fix my issue. does anyone have any suggestions?

Thanks!

If needed, this is the tutorial I'm following: https://djangoforbeginners.com/hello-world/

4
Did you add pages to your INSTALLED_APPS in settings.py as described in that tutorial?Selcuk
@selcuk yes I did and then rechecked and checked again because I thought that was the issue too! It's there. :/bziggy
Do you have a file named __init__.py in pages folder?Selcuk
Have you tried importing with the config (and defining the config? ) ex ` INSTALLED_APPS = [ 'pages.apps.PagesConfig' # ... ] `wdfc
You don't normally 'run' url.py. Are you starting your app with ./manage.py runserver? See docs.djangoproject.com/en/2.0/intro/tutorial01Keith John Hutchison

4 Answers

0
votes

Since the urls file is already in the app pages you can't import it using the said name. I would suggest you change from pages import views to from . import views

0
votes

since the urls.py and and views.py in the same directory therefor you can simply do from . import views it will import all the views.

0
votes

I had the same problem minutes ago, but the only issue which caused this same problem for me was a typo in configuring my app in the settings.py instead of 'pages.apps.PagesConfig' i wrote 'pages.apps.pagesconfig' and then the problem was solved frequently quickly.

0
votes

The exact same thing happened with me today, turns out i was missing the comma after 'pages.apps.PageConfig'. Super silly mistake