0

Rather than using environmental variables to hide sensitive information, such as, SECRET_KEY, I'm using a module called local_settings.py since the file is ignored by gitignore.

Within settings.py, I imported the module as from .local_settings import *

I'm using PythonAnywhere as the means of deploying my website yet when python manage.py collectstatic is executed in its console the following error is raised:

    from .local_settings import *
ModuleNotFoundError: No module named 'stackoverflow_clone.local_settings'

Is this error occuring because local_settings.py is being treated as if it doesn't exist at all? How can the error be resolved so that configuration such as SECRET_KEY can be imported?

This directory structure reflects what's on my local machine.

enter image description here

(stackoverflow_clone-virtualenv) 23:19 ~ $ pwd
/home/ittybitty

(stackoverflow_clone-virtualenv) 23:19 ~ $ ls
README.txt  django_stackoverflow

(stackoverflow_clone-virtualenv) 23:19 ~ $ cd django_stackoverflow
(stackoverflow_clone-virtualenv) 23:19 ~/django_stackoverflow (main)$ ls -l
-rw-rw-r-- 1 ittybitty registered_users   22 Feb 12 19:09 README.md
drwxrwxr-x 7 ittybitty registered_users 4096 Feb 12 19:09 authors
-rw-rw-r-- 1 ittybitty registered_users  675 Feb 12 19:09 manage.py
drwxrwxr-x 4 ittybitty registered_users 4096 Feb 12 19:09 node_modules
-rw-rw-r-- 1 ittybitty registered_users  369 Feb 12 19:09 package-lock.json
-rw-rw-r-- 1 ittybitty registered_users  569 Feb 12 19:09 package.json
-rw-rw-r-- 1 ittybitty registered_users 8581 Feb 12 19:09 paginated_db_set.json
drwxrwxr-x 7 ittybitty registered_users 4096 Feb 12 19:09 posts
-rw-rw-r-- 1 ittybitty registered_users  107 Feb 12 19:09 requirements.txt
drwxrwxr-x 3 ittybitty registered_users 4096 Feb 12 23:12 stackoverflow_clone
drwxrwxr-x 2 ittybitty registered_users 4096 Feb 12 19:09 templates
drwxrwxr-x 4 ittybitty registered_users 4096 Feb 12 19:09 web_assets

(stackoverflow_clone-virtualenv) 23:19 ~/django_stackoverflow (main)$ ls -l stackoverflow_clone
total 20
-rw-rw-r-- 1 ittybitty registered_users    0 Feb 12 19:09 __init__.py
drwxrwxr-x 2 ittybitty registered_users 4096 Feb 12 23:13 __pycache__
-rw-rw-r-- 1 ittybitty registered_users  415 Feb 12 19:09 asgi.py
-rw-rw-r-- 1 ittybitty registered_users 4034 Feb 12 23:12 settings.py
-rw-rw-r-- 1 ittybitty registered_users 3247 Feb 12 19:09 urls.py
-rw-rw-r-- 1 ittybitty registered_users  415 Feb 12 19:09 wsgi.py

/var/www/ittybitty_pythonanywhere_com_wsgi.py

# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys

# assuming your django settings file is at '/home/ittybitty/mysite/mysite/settings.py'
# and your manage.py is is at '/home/ittybitty/mysite/manage.py'
path = '/home/ittybitty/django_stackoverflow'
if path not in sys.path:
    sys.path.append(path)

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

# then:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

binny
  • 649
  • 1
  • 8
  • 22
  • 2
    Your screenshot shows the file structure on your local machine, and it looks fine. Perhaps you could also share a screenshot of the equivalent structure deployed on PythonAnywhere? It would also be useful to know the sys.path setup that you've put into your WSGI file (the one that you access from the "Web" page inside PythonAnywhere, not the one in stackoverflow_clone) – Giles Thomas Feb 12 '23 at 16:09

1 Answers1

0

It looks like the file local_settings.py does not exist on PythonAnywhere, and your settings.py is trying to import from it. Because you (correcly!) did not add local_settings.py to your git repo, and put it in .gitignore, you'll need to create one on PythonAnywhere, and fill it in with the appropriate stuff.

Giles Thomas
  • 6,039
  • 2
  • 33
  • 51