Forums

Deploying django project error

I'm desperate. I'm trying to deploy an existing django app on pythonanywhere, and I stucked at the popular error: ModuleNotFoundError: No module named 'sw.settings'. My WSGI (at var/www):

import os
import sys

path = '/home/SamperMan/sw'  # There is manage.py
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'sw.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

I've read DebuggingImportError page and everything here works fine:

(mysite-virtualenv) 14:14 ~/sw (master)$ python -i /var/www/samperman_pythonanywhere_com_wsgi.py
>>> import sw.settings
>>> sw.settings.BASE_DIR
'/home/SamperMan/sw'
>>>

I've configured web app settings (set a Source code and VirtualEnv settings), installed django on the new virtualenv. I really don't know where could I miss something? What mistake can it be?

Try:

path = '/home/SamperMan/sw'  # There is manage.py
if path not in sys.path:
    sys.path.insert(0, path)

for adding the path instead.

It works. But why?

Because you have /home/SamperMan on your sys.path, so when you import sw, you're importing the top-level sw module, not the one inside it that has your settings.

Got it, thanks a lot.