简体   繁体   中英

How to fix Django "ModuleNotFoundError: No module named 'application'" on AWS?

I am trying to redeploy a Django web application on AWS. My elastic beanstalk environment has been red a couple of times. When I ran eb logs on the cli, I am getting a "ModuleNotFoundError: No module named 'application' error". I think this has got to do with my wsgi configuration.

I have deployed this web app on AWS before. I messed up when I tried deploying a new version then decided to just start over. Here is my wsgi.py configuration:

```import os

from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler

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

application = StaticFilesHandler(get_wsgi_application())```

When I deploy the app, it's giving me a 502: Bad gateway error. Let me know if you would like more info on the issue. Any pointers would be greatly appreciated.

By default, Elastic Beanstalk looks for a file named application.py to start your application. Because this doesn't exist in the Django project that you've created, you need to make some adjustments to your application's environment. You also must set environment variables so that your application's modules can be loaded.

follow these instructions: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html#python-django-configure-for-eb

I got this error because in an older version the django.config looked like this:

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: ebdjango/wsgi.py

and now it should look like this:

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: ebdjango.wsgi:application

I faced this issue as well. In my case I was getting a 502 nginx error after deploying to AWS EB using eb deploy and my environment had a red flag.

My AWS EB was using Amazon Linux 2 AMI, everything was set correctly BUT after lot of tries and errors I realized my .ebextensions folder was named .ebtextensions .

I wish EB or django would have indicated on its logs that my folder was nanmed incorrectly.

I got this error and also spent days trying to solve it. The problem is from the django.config file.

option_settings:
  aws:elasticbeanstalk:container:python:
  WSGIPath: <app name>.wsgi:application

The first value (app name) in the WSGIPath option needs to be the directory that contains the wsgi.py file

Check the WSGIPath in elastic beanstalk configuration (Software Category) in the AWS console too.

It should be

<app_name>.wsgi:application

The "ModuleNotFoundError: No module named 'application'" error in Django is typically caused by a problem with your Python import path. Here are some steps you can take to fix this issue on AWS:

1)Verify that your Django project's settings.py file has the correct path to your application. Make sure that the path is correct and that the application name is spelled correctly.

2)Check that the application folder is inside your project folder and that the __init__.py file is present in the application folder.

3)Check that the application is correctly included in the INSTALLED_APPS list in your project's settings.py file. Make sure the name of the app is spelled correctly and that it's in the correct case.

4)If you're using a virtual environment, make sure that the virtual environment is activated, and that the required dependencies are installed in it.

5)Make sure that all the dependencies of your application are installed in the correct environment and versions. You can check this by running pip freeze command in the terminal and check the versions of the packages

6)Restart the web server after making any changes.

7)If the problem persists, check the logs of your web server and the output of the Django management commands to see if there are any additional error messages that might provide more information about the cause of the problem.

8)If the problem is still not fixed, you can try creating a new virtual environment, install all the dependencies again and run the project again.

By following these steps, you should be able to resolve the "ModuleNotFoundError: No module named 'application'" error and get your Django application running on AWS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM