简体   繁体   中英

ModuleNotFoundError: No module named 'django' when deploying to Elastic Beanstalk

I have been working on a Django project locally and I finally decided to release it to AWS Elastic Beanstalk and during the deployment process I keep receiving an error saying:

from django.core.wsgi import get_wsgi_application

No module named 'django'

I have walked through the official tutorial from AWS as well as the tutorial from the Real Python I have verified that Django is in fact installed by running pip freeze and it returns

...

colorama==0.3.9

Django==2.2.9

django-celery==3.3.1

....

Also I've been working with Django extensively locally. But just to be sure I ran the following command and got this output.

(.venv) $ source .venv/bin/activate

(.venv) $ pip install django

Requirement already satisfied: django in ./.venv/lib/python3.7/site-packages (2.2.9)

Requirement already satisfied: sqlparse in ./.venv/lib/python3.7/site-packages (from django) (0.3.0)

Requirement already satisfied: pytz in ./.venv/lib/python3.7/site-packages (from django) (2019.3)

And when I run:

(.venv) $ python
Python 3.7.4 (default, Jul  9 2019, 18:13:23) 
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.__file__)
.../code/core_web_app/.venv/lib/python3.7/site-packages/django/__init__.py

Sorry if this is over kill but most of the post I have looked at have comments asking if Django is installed or saying that Django must be installed. My presumption is that Django is installed on my local machine but not on my remote. Furthermore, I have noted this post and made sure that my requirements.txt file is in my root directory as well. Below you may find some additional information.

.ebextensions/django.config

    option_settings:
  "aws:elasticbeanstalk:application:environment":
    DJANGO_SETTINGS_MODULE: "myapp.settings"
    "PYTHONPATH": "/opt/python/current/app/src:$PYTHONPATH"
  "aws:elasticbeanstalk:container:python":
    WSGIPath: src/myapp/wsgi.py
    NumProcesses: 3
    NumThreads: 20
  "aws:elasticbeanstalk:container:python:staticfiles":
    "/static/": "www/static/"

src/myapp/wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')

application = get_wsgi_application()

Any help would greatly be appreciated as this is my first time working with this and I presume I am making a really silly mistake somewhere.

Well to answer your question, there are few things that you have to check out while deploying your Django or flask or dash application using AWS ElasticBeanstalk(EBS).

NOTE: The following steps are outlined if the application is hosted using AWS EBSCLI ie the command line feature of EBS and not the method of hosting through the User Interface of the EBS on your AWS account. On my personal experience, I have found that the UI method of hosting the application will not work for flask, Django or dash applications.

  1. While hosting your application into EBS, check the hosting logs if you have got any errors. Even though the EBS prompts you that you have hosted your application successfully, you will have to cross check on the logs to find any errors.
  2. Once you have verified you have no errors in Step 1, navigate to the logs console of your EBS application, download entire logs and then check for any other errors. If you have any errors, you have to rectify them first.
  3. Now that you have said that the AWS Linux machine is not able to import the Django package, it might be that you are checking for the Django package manually under a different virtual environment whereas the AWS EBS has hosted your application under another virtual environment.
  4. To solve Step 3, it happens to be that any application that is hosted using AWS EBS on a linux server is hosted under the following virtual environment path. /opt/python/run/venv/bin . In order to verify this, just type the command cd /opt/python/run/venv/bin after connecting to your linux server. Once you're able to navigate to this path, try activating the virtualenv by giving source ./activate
  5. Once you've activated this virtualenv that AWS EBS has created for your application, try starting python inside this virtualenv and then try import django . If you find that django package is not installed under this virtualenv, you have to install it manually, since this is the virtualenv that your hosted application will refer to. And any new packages that need to be referenced by your hosted application will have to be installed under this virtualenv only.
  6. To further give you additional information, your hosted application will reside on the AWS linux server under this directory /opt/python/bundle/2/app . You can refer to this directory if you are willing to alter your application code or any supporting files in the future. For example, if you want to change any of the source code, you can directly edit the source code files under this repository and restart your server for the changes to take effect.

Update: Since you've specified that during the hosting of your application, you have received the error on the django package not found, please follow the following steps to rectify this error.

  1. While creating your requirements.txt file, make sure that only the necessary packages for your application are present inside that file. When you do pip freeze requirements.txt , all the packages which are present in your current environment will make an entry in requirements.txt file. This might create errors while AWS EBS is trying to install this file. The reason is, say for example, PackageA and PackageB might be dependent packages of PackageC. If you install PackageC, automatically PackageA and PackageB would get installed. Having said this, you have to remove PackageA and PackageB in your requirements.txt file and keep only PackageC. Because installation of PackageC would automatically install it's dependent packages which are PackageA and PackageB. So to keep it short, you only have to include the packages that you manually installed using pip install in your local machine inside your requirements.txt file. All other packages will be dependent packages of these packages and will install automatically when the master package is installed. So these dependent packages need to be removed from requirements.txt .
  2. If you're still receiving the same error, once the application is hosted with errors, try to connect to your AWS Linux machine using CLI(Command Line Interface), navigate to the virtual environment path by doing cd /opt/python/run/venv/bin , activate it using source ./activate , invoke python interpreter and then try importing the packages manually. If you find any of the packages are uninstalled, you can install them manually and restart the server for the changes to take effect.

For more details, you can refer this link . Though this is created for hosting a flask application, hosting a django application will also work on same lines.

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