lekarane
lekarane

Reputation: 1

Python ModuleNotFoundError: No module named

I finally had to post this here after, embarrassingly, being stuck for almost 2 days. I have been trying to import a file from a directory on same level as my parent directory. Here is my project structure:

website
├──pages
│  └──views.py
└──website
   └──urls.py

You can see a more detailed structure from the image attached. I am trying to import views.py in urls.py with: from website.pages import views

This gives the error: ModuleNotFoundError: No module named 'website.pages'. I tried using relative import as well: from ..pages import views I got an error here as well: ImportError: attempted relative import beyond top-level package

Please help! Code

After renaming After renaming

Upvotes: 0

Views: 2176

Answers (2)

SaGaR
SaGaR

Reputation: 542

As i can see from your pic . You seems to have your project name as website and also your app name as website. Django firstly tries to find the module in your project and not in your app. To correct this you have to import using:

from pages import views

But still that is a bad practice to name your project and your app the same name. Plz change your top level directory name.

Upvotes: 0

Frank
Frank

Reputation: 1249

This is because the name 'website' of your repository is the same as the name you call.

enter image description here

To solve the problem:

enter image description here

Upvotes: 3

Related Questions