0

I just creating a simple backend and everything working fine until I create this python filepopulate_fake_script.py when I'm trying to run this it's showing ModuleNotFoundError: No module named And I've already checked other similar questions but unable to solve. Please help me to fix this. And you can see all files in here https://github.com/crajygemer/Django_Stuff/tree/master/ten_project

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ten_project.settings')

import django
django.setup()

# Fake Pop Script
import random
from ten_project.ten_app.models import AccessRecord, Webpage, Topic
from faker import Faker
fakegen = Faker()
topics = ['Search', 'Social', 'Marketplace', 'News', 'Games']

# call Topic Model
def add_topic():
    t = Topic.objects.get_or_create(top_name=random.choice(topics))[0]
    t.save()
    return t

# call rest of Models
def populate(N=5):

    for entry in range(N):

        # get the topic for the entry
        top = add_topic()

        # add fake data for each entry
        fake_url = fakegen.url()
        fake_name = fakegen.company()
        fake_date = fakegen.date()

        # fake entry for webpage model
        webpg = Webpage.objects.get_or_create(
            topics, url=fake_url, name=fake_name)[0]

        # fake entry for access record
        acc_rec = AccessRecord.objects.get_or_create(
            name=webpg, date=fake_date)[0]


if __name__ == '__main__':
    print('Run Fake Script')
    populate(20)
    print('Run Successfully.')

2 Answers2

0

You are getting a ModuleNotFoundError because your script cannot import this:

from ten_project.ten_app.models import AccessRecord, Webpage, Topic

You should take a look at this: How to import the class within the same directory or sub directory?.

Klatten
  • 307
  • 3
  • 16
0

In Django absolute imports works in respect to the manage.py. You have your manage.py in ten_project directory so that means your import should be

from ten_app.models import AccessRecord, Webpage, Topic
mursalin
  • 1,151
  • 1
  • 7
  • 18