Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DJANGO_SETTINGS_MODULE No Module Named

I am trying to create a population script for my database, but I get a No module named error.

Project Structure:
staticone/
    manage.py
    mysite/
      populate_rango
      settings
      urls
      __init__
      wsgi
      rango/    

Traceback:

Traceback (most recent call last):
File "populate_rango.py", line 8, in <module>
django.setup()
File "/home/ubuntu/virtenv/webstatic/lib/python3.5/site-
packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/ubuntu/virtenv/webstatic/lib/python3.5/site-
packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/ubuntu/virtenv/webstatic/lib/python3.5/site-
packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'mysite'

My path:

 ['/home/ubuntu/virtenv/webstatic/staticone/mysite', 
 '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-
 x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', 
 '/home/ubuntu/virtenv/webstatic/lib/python3.5/site-packages']

My settings.py file is under mysite

populate_rango.py under mysite folder:

 import sys
 print (sys.path)
 import os
 os.environ.setdefault('DJANGO_SETTINGS_MODULE','settings')
 import django
 django.setup()
 from mysite.rango.models import Category,Page

If I try

os.environ.setdefault('DJANGO_SETTINGS_MODULE','mysite.settings')

I get the same error:

No module named mysite.

mysite is on the path, so I am confused as to why it is not finding the settings file.

like image 753
Jahmul14 Avatar asked Aug 17 '17 14:08

Jahmul14


1 Answers

I needed to put the parent directory of mysite on the path. I only had /home/ubuntu/virtenv/webstatic/staticone/mysite. Now it works when I included /home/ubuntu/virtenv/webstatic/staticone/.

import sys
sys.path.append("/home/ubuntu/virtenv/webstatic/staticone/")
print (sys.path)
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','mysite.settings')
import django
django.setup()
from mysite.rango.models import Category,Page

Now it works.

like image 132
Jahmul14 Avatar answered Sep 20 '22 13:09

Jahmul14