Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

django - No module named 'mysite.settings'

I know this question has been asked quite a lot but all the solutions given do not seem to fit for me.

I run this in a venv with python 3.6.8 and django 2.2.10 When I run django from cli it works and all functionality is working great so I know it is not django itself that is failing me.

Path to wsgi script is "/opt/sites/aws/okta/wsgi.py"

Actual wsgi.py:

import os, sys
sys.path.append('/opt/sites/aws')
#path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#sys.path.append(path)
print(sys.path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "okta.settings")

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

When running python to see what errors it gives I get

ModuleNotFoundError: No module named 'okta.settings'

I get the same error when running apache with debug logging.

I have quadruple checked the path and the environment variables and they are set right in the wsgi.py script.

Asked for the INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'okta_oauth2.apps.OktaOauth2Config',
    'ops.apps.OpsConfig'
]

aws/
    bin/
    lib/
    lib64/
    logs/
    okta/
        __init__.py
        urls.py
        settings.py
        wsgi.py
    okta_oauth2/
    ops/
    share/
    static/
    manage.py
    pyvenv.cfg

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Although I did not figure out what was wrong. This is what I did to resolve.

Note for anyone reading this in the future, please DO NOT do this first. I tried other more common results like ImportError: No module named mysite.settings (Django) to fix the issue first and when none worked I tried this.

cd /opt/sites #Just outside the venv
sudo service httpd stop # stop web server
mv aws aws_broken_20201223 # move your broken env to a new name but do not delete
python3 -m venv aws #create new venv 
cp -R aws_broken_20201223/(django dirs) aws/ #copy all the custom apps and project you have to the new folder
cd aws #go to the new install
bin/activate #activate your env
pip 3 install -r requirements.txt # I had a predefined requirements.txt for all mods I needed. I would suggest you do the same before doing this. 
python3 okta/wsgi.py # Test to see if you are still getting the not found error 
sudo service httpd start # start web server back up

voila it works without issue, no config change at all. Very strange but I'll take a working production site then a broken one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...