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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With