Forums

ImportError: No module named 'mysite.settings'

I was deploying django app to PythonAnywhere. Environment I am using is: Python 3.4 Django 1.8

In _pythonanywhere_com_wsgi.py, I have the following code

import os

import sys

path = '/home/ZNYAZO/my-first-blog'  # use your own username here
if path not in sys.path:
    sys.path.append(path)

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

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

I try to run the thing, and it gives me the following error log

ImportError: No module named 'mysite.settings'

[edited by admin: formatting]

Your help is greatly appreciated

you need to make sure your path is set correctly.

path = '/home/ZNYAZO/my-first-blog/mysite'

Gracias :D

I'm having the same problem, Python 3.4 and Django 1.9:

ImportError: No module named 'charlesvogl.settings'

I've gone through all the troubleshooting tips I can find in the forums and the document on debugging sys path (https://help.pythonanywhere.com/pages/DebuggingImportError/), no success.

My wsgi file is below:

import os
import sys
path = '/home/bjorncooley/charlesvogl_sandbox'
if path not in sys.path:
    sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'charlesvogl.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

My file structure conforms to structure of the WSGI file:

'/home/bjorncooley/charlesvogl_sandbox' is the project directory, and 'charlesvogl' is the primary app directory that contains settings.py. So, path to settings is:

'/home/bjorncooley/charlesvogl_sandbox/charlesvogl/settings.py'

I can run the wsgi.py file successfully from command line

When I run the wsgi file from command line, I can import the settings file without any issues. Steps below:

workon charles_sandbox2
python -i /var/www/sandbox_charlesvogl_com_wsgi.py
import charlesvogl.settings

No errors thrown.

Running the settings file from the command line throws an error about performing relative imports, but I get the same error running the settings file from command line for other apps I have on PA that are successfully deployed, so that doesn't seem to be helpful.

Any thoughts would be greatly appreciated!

I thought it might be that Django 1.9 handles imports and settings differently so that our instructions are out-of-date, but I've just successfully created an empty DJango 1.9 web app that works fine. Perhaps there's an issue with one of the modules you use or your apps.

One possibility that just occurred to me that you could try:

workon charles_sandbox2
python -i /var/www/sandbox_charlesvogl_com_wsgi.py
import django
django.setup()

Failing that, could you try creating an empty Django project and then move stuff into it until it fails? That would narrow down the search space.

Hi Glenn, thanks for the response, that put me on the right track - django.setup() worked fine, but working through installing another app I realized that the WSGI_APPLICATION constant in my the settings.py file had a typo. Fixed that and everything came up.

Excellent. I'll see if I can work up a diagnostic to add to the debugging page. Thanks for the pointer.

That's just weird. I deliberately broke my WSGI_APPLICATION constant so I could try working up a diagnostic and the damn website still works.

You're right, I'm getting the same behavior. What I actually did to solve the problem was change the directory name of my project to match the name in the WSGI_APPLICATION setting, rather than changing the setting itself.

Full backstory, this may help: I originally created the project with the name "charlesvogl." When I deployed I changed the project directory name to "charlesvogl_sandbox", and set the WSGI file to use the "charlesvogl_sandbox" directory. This is the state where it was throwing errors. I noticed in my settings file that the WSGI_APPLICATION constant was still set to "charlesvogl", so I changed the project directory name to match. Once I did that, the errors went away.

With other projects I've been able to change the project directory name without causing issues, so I assumed it was a problem with the WSGI naming conventions. But maybe there's something deeper going on with changing project directory names I'm not aware of?

I solved the problem by replacing the original append with:

sys.path.insert(0, path)

During the problem, I was able to do all the checks in the https://help.pythonanywhere.com/pages/DebuggingImportError/

.

import os
import sys
path = '/home/bjorncooley/charlesvogl_sandbox/add_the_django_project_name'
if path not in sys.path:
    sys.path.insert(0, path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'charlesvogl.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

First I added "sys.path.insert(0, path)" it didn't work then I added my django project name to the path "add_the_django_project_name'" the i went to relode the web in PythonAnywhere then i went to my website url . there i hard reloded , from control shift i in windows , right click then Hard reload

[edit by admin: formatting]

OK -- is it working now?

yes it is thank's

https://zainsyed.pythonanywhere.com/

Glad to hear that!

Hello Guys, after spending some time reading your replies I have some further tips if new people are facing this problem.

Tip 1: Before trying anything else go to the web console (where you specified your vritual envs, paths etc..) and you will find a section "Code:" and in there you will find a link "Go to directory". Follow the files until you get to settings.py and the whole path will be shown up.

Tip 2: THE PATHS ARE CASE SENSITIIIVE (believe it or not)

The end result for my path was: /home/innoservices/InnoServices/UI_APP/UI_APP

(try to guess how much time i spent trying to debug this path, hint: less than building the app)