raviraj
raviraj

Reputation: 375

Django ModuleNotFoundError can't import model file

I am new to Django and want to create dummy data so using faker library with sqlite db. Created models.py and DummyData.py .

models.py

from django.db import models
# Create your models here.
class User(models.Model):
    name = models.CharField(max_length=200)
    surname = models.CharField(max_length=200)
    email = models.EmailField(max_length=200, unique= True)

    def __str__(self):
        return "Object created " + "Name :-" + self.name + " Surname :-" + self.surname + " Email :-" + self.email

for creating fake data made DummyData.py

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'Course.settings')

# 1. method
from .models import User

# 2. method
from appTwo.models import User

Both ways I am not able to import Users class from models getting error like

Method 1 output    
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package

Method 2 output 
ModuleNotFoundError: No module named 'appTwo

created empty init.py in appTwo folder. File structure is as above File Structure

But I can able to import models.py using method 2 in Terminal, So I am little bit confuse.

Terminal result

Upvotes: 0

Views: 584

Answers (1)

Yves Hary
Yves Hary

Reputation: 312

I would recommend to initialize django in the correct way:

from os import environ as env

if not 'DJANGO_SETTINGS_MODULE' in env:
    env.setdefault('DJANGO_SETTINGS_MODULE', 'Course.settings')

import django #  This is important
django.setup() #  !!!

Upvotes: 1

Related Questions