【问题标题】:ModuleNotFoundError: No module named 'djangoModuleNotFoundError:没有名为“django”的模块
【发布时间】:2019-03-08 21:55:00
【问题描述】:

我使用 django 在虚拟环境中构建了一个站点,并按照 AWS 文档中的步骤部署该站点。我已经使用 Elastic Beanstalk 将我的站点部署到 AWS Web 服务器,并设置了一个运行 3.6 和 django 2.1.1 的 python 环境。我已提取日志并收到以下错误:

Traceback (most recent call last):
File "/opt/python/current/app/weddingProject/wsgi.py", line 12, in <module>
from django.core.wsgi import get_wsgi_application
ModuleNotFoundError: No module named 'django'
Target WSGI script '/opt/python/current/app/weddingProject/wsgi.py' cannot be loaded as Python module.

我已经阅读了其他帖子,他们说没有安装 django,但是 requirements.txt 文件确实将它列为要安装的包。所以我不确定是什么导致了问题,我不知道如何检查服务器以确保它已安装。

当我运行 pip freeze > requirements.txt 时,我确实看到 django 被列为要安装的包。我运行 eb deploy 并且由于某种原因没有安装 django。

【问题讨论】:

  • "...我不确定是什么导致了问题" - 显然,未安装的模块导致了问题。 Django 的文档中描述了安装过程。我想AWS也提供各种安装手册。
  • 您是否尝试过手动安装 django 并在您这样做时检查它是否有效?
  • 是的,可能没有安装 Django。这可能会有所帮助:stackoverflow.com/a/10429168/5548239
  • 呃... requirements.txt 列出了 Django,但是你真的运行 install 命令来安装 requirements.txt 中的包吗?
  • @ForceBru 显然它没有安装,但为什么我想弄清楚。按照 aws 文档的说明进行操作。 docs.aws.amazon.com/elasticbeanstalk/latest/dg/…

标签: python django python-3.x amazon-web-services amazon-elastic-beanstalk


【解决方案1】:

我有同样的问题。文件 requirements.txt 比它应该的更深一层。将requirements.txt.elasticbeanstalk/放在同一目录下

例子:

├── .ebextensions
│   ├── 01_packages.config
│   └── 02_python.config
├── .elasticbeanstalk
│   └── config.yml
├── testproject
│   ├── db.sqlite3
│   ├── cust_app
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── manage.py
│   └── testproject
│       ├── asgi.py
│       ├── __init__.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
└── requirements.txt

【讨论】:

  • 如果遇到这种情况,请检查您是在项目文件夹内还是项目文件夹外进行压缩,打开或解压缩时的压缩文件应与此解决方案类似
【解决方案2】:

不知道为什么,但 elasticbeanstalk 没有安装要求。它也适合我,所以我必须手动完成。在适当的环境中安装需求的适当方法是:

  1. 在 ebs 使用的 ec2 实例中登录。
  2. 转到 /var/app/venv/staging-LQM1lest/bin
  3. sudo ./pip install -r /var/app/current/requirements.txt

这应该可行。

【讨论】:

    【解决方案3】:

    我遇到了完全相同的问题。对我来说,我需要把我的.elasticbeanstalk 和它的config.yml 放在mysite/ 中。然后它起作用了。 WSGIPath 应该是 mysite/wsgi.py,而不是 mysite/mysite/wsgi.py

    【讨论】:

      【解决方案4】:

      发现问题。 requirements.txt 文件被放置在错误的目录中,导致 Django 无法安装。

      【讨论】:

      • 你需要把它放在哪里??
      【解决方案5】:

      https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html

      1. 创建环境并使用 eb create 将应用程序部署到该环境: eb create django-env

      您是否创建了虚拟环境?如果是,您会为该命令获得什么输出 eb status.

      或者您能否列出使用 Elastic Beanstalk 部署 Django 应用程序所遵循的步骤?

      阅读此文档:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-container.html 以使用 AWS Elastic Beanstalk Python 平台。

      【讨论】:

        猜你喜欢
        • 2021-08-06
        • 2019-11-28
        • 2018-11-03
        • 2019-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多