Sabin Sapkota
Sabin Sapkota

Reputation: 36

ModuleNotFoundError while importing a class from different app

In my django project LibraryManagement I have two apps 'book' and 'authors'. I am trying to establish relationship between two apps using foreign key. While I try to import class Authors in the book I got error ModuleNotFound:No module named 'LibraryManagement.authors'

Below is my project structure

LMS
 -LibraryManagement
   -authors
   -book
   -LibraryManagement
 -venv

Code of models.py from authors app

from django.db import models

class Authors(models.Model):
   author_name = models.CharField(max_length=100)
   description = models.TextField(max_length=300)

Code of models.py from book app

from django.db import models
from LibraryManagement.authors.models import Authors

class Book(models.Model):
    book_name = models.CharField(max_length=100)
    author = models.ForeignKey(Authors)
    remarks = models.TextField(max_length=300)

Upvotes: 0

Views: 114

Answers (1)

Thiago Salvatore
Thiago Salvatore

Reputation: 311

You are creating those files within packages. Packages in python only works with a __init__.py file inside it. So, if you want to be able to do:

from LibraryManagement.authors

LibraryManagement has to have a __init__.py file within it. The same happens within "authors". It has to have a __init__.py file so you can import it as a package.

The __init__.py file can be empty, but they have to exist.

Upvotes: 1

Related Questions