Forums

ModuleNotFoundError: No module named 'blog'

Hello there, first of all thank you for this service.

I have cloned my GitLab repo to host it here. The directory tree is as such:

.
└── myblog
    ├── blog
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-310.pyc
    │   │   ├── __init__.cpython-39.pyc
    │   │   ├── admin.cpython-310.pyc
    │   │   ├── admin.cpython-39.pyc
    │   │   ├── apps.cpython-310.pyc
    │   │   ├── apps.cpython-39.pyc
    │   │   ├── models.cpython-310.pyc
    │   │   ├── models.cpython-39.pyc
    │   │   ├── urls.cpython-310.pyc
    │   │   ├── urls.cpython-39.pyc
    │   │   ├── views.cpython-310.pyc
    │   │   └── views.cpython-39.pyc
    │   ├── admin.py
    │   ├── apps.py
    │   ├── migrations
    │   │   ├── 0001_initial.py
    │   │   ├── __init__.py
    │   │   └── __pycache__
    │   │       ├── 0001_initial.cpython-310.pyc
    │   │       └── __init__.cpython-310.pyc
    │   ├── models.py
    │   ├── tests.py
    │   ├── urls.py
    │   └── views.py
    ├── db.sqlite3
    ├── manage.py
    ├── myblog
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-310.pyc
    │   │   ├── __init__.cpython-39.pyc
    │   │   ├── settings.cpython-310.pyc
    │   │   ├── settings.cpython-39.pyc
    │   │   ├── urls.cpython-310.pyc
    │   ├── asgi.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── requirements.txt
    └── templates
        ├── base.html
        ├── index.html
        ├── post_detail.html
        └── sidebar.html

Initially I had problems with myblog.settings module not found, which I could solve with changing it to myblog.myblog.settings in wsgi.

However I'm now stuck with this error:

Error running WSGI application
ModuleNotFoundError: No module named 'blog'
  File "/var/www/pesekcuy_pythonanywhere_com_wsgi.py", line 89, in <module>
    application = get_wsgi_application()

  File "/home/pesekcuy/.virtualenvs/myvirtualenv/lib/python3.9/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
    django.setup(set_prefix=False)

  File "/home/pesekcuy/.virtualenvs/myvirtualenv/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)

  File "/home/pesekcuy/.virtualenvs/myvirtualenv/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)

  File "/home/pesekcuy/.virtualenvs/myvirtualenv/lib/python3.9/site-packages/django/apps/config.py", line 223, in create
   import_module(entry)

Are there something I do wrongly? I have DuckDuckGo-ing and StackOverflow-ing and the answer suggested to include an empty __init__.py in blog folder, but I already have it.

Make sure that your directory structure looks really like in the structure above. Also -- how did you set up paths in the wsgi file?

Make sure that your directory structure looks really like in the structure above.

That structure is the output of tree command in my working git directory (/home/pesekcuy/myblog). I just copied it from the console

how did you set up paths in the wsgi file?

I just uncommented django section of the default wsgi file as such:

# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys
#
## assuming your django settings file is at '/home/pesekcuy/mysite/mysite/settings.py'
## and your manage.py is is at '/home/pesekcuy/mysite/manage.py'
path = '/home/pesekcuy/myblog'
if path not in sys.path:
    sys.path.append(path)
#
os.environ['DJANGO_SETTINGS_MODULE'] = 'myblog.myblog.settings'
#
## then:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Do I need to change the path as well?

Thanks for your insight, @pafk. It turns out that my wsgi is wrong.

I just changed the path to one folder inside the git directory

path = '/home/pesekcuy/myblog/myblog'

and the os.environ line as such

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

Thank you and sorry for bothering!

No problem -- glad you got that working!