Before installing Django, I installed virtual environment with conda
. After activating my virtual environment installed django pip install django
. When I hit python on the console there is no error after importing python. However, when I run the python pop_script.py
I get
(MyDjangoEnv) MacBook-Pro:firstProject eLmaesTro$ python pop_script.py
Traceback (most recent call last):
File "pop_script.py", line 4, in <module>
import django
ModuleNotFoundError: No module named 'django'
I could not execute this file.
Here is the code of file. (pop_script.py)
import os
os.environ.setdefault('DJANGO_SETTINGS_VALUE','firstProject.settings')
import django
django.setup()
# FAKE POP SCRIPT
import random
from firstApp.models import Topic,Webpage,AccessRecord
from faker import Faker
fakegen = Faker()
topics = ['Search','Social','Marketplace','News','Games']
def add_topic():
t = Topic.objects.get_or_create(top_name=random.choice(topics))[0]
t.save()
return t
def populate(N=5):
for entry in range(N):
# get the topic
top = add_topic()
# create the fake data for that entry
fake_url = fakegen.url()
fake_date = fakegen.time()
fake_name = fakegen.company()
# create the new webpage entry
webpg = Webpage.objects.get_or_create(topic=top,url=fake_url,name=fake_name)[0]
# create a fake access record for that webpage
acc_rec = AccessRecord.objects.get_or_create(name=webpg,date=fake_date)[0]
if __name__ == '__main__':
print('populating script!')
populate(20)
print('population complete!')