编辑wsgi.py无法修复错误,ImportError:没有名为django.core.wsgi的模块

时间:2018-08-21 15:17:59

标签: python django gunicorn

我和这里有同样的问题:

ImportError: No module named django.core.wsgi Apache + VirtualEnv + AWS + WSGI

但是该解决方案不适用于我。

我实际上复制了此处提供的更精致的示例:

https://pseudoscripter.wordpress.com/2015/10/23/import-error-no-module-named-django-core-wsgi/

我完全搞砸了以前的EC2实例,所以我决定创建一个新的EC2实例并重新开始。

我运行了一些命令来设置服务器:

yum upgrade -y 

yum update -y

yum search python3 | grep devel

yum install python3-pip.noarch -y 

yum install emacs -y

yum install make automake gcc gcc-c++ kernel-devel -y 

yum install git -y

yum install nginx -y

yum install postgresql-devel  -y 

yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel libxslt-devel libxml2-devel -y

yum install openssl-devel -y

yum install python-virtualenv.noarch

amazon-linux-extras install nginx1.12

然后我做了一些设置Nginx的工作:

How to install nginx 1.9.15 on amazon linux disto

sudo chmod 755 /etc/rc.d/init.d/nginx

sudo service nginx start

sudo chkconfig nginx on

然后我使用scp将Django应用复制到以下路径:

/usr/share/lynette-ecomerce-demo

然后,以常规的EC2用户ec2-user身份运行:

source bin/activate

pip install Django

pip install gunicorn

如果我运行python,然后执行以下操作:

>>> import sys
>>> print(sys.prefix)

/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django

我没有使用pip3,因为这似乎是上次造成所有问题的原因。但是现在我做到了:

gunicorn  ecommerce.wsgi:application --bind 0.0.0.0:8000

我收到“ ImportError:没有名为django.core.wsgi的模块”错误,因此我按照其他地方给出的建议打开了该文件:

ecommerce/wsgi.py

我这样编辑它:

"""                                                                                                                                                                      
WSGI config for ecommerce project.                                                                                                                                       

It exposes the WSGI callable as a module-level variable named ``application``.                                                                                           

For more information on this file, see                                                                                                                                   
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/                                                                                                             
"""

import os
import sys

sys.path.append("/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ecommerce.settings")


from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

但是现在,如果我这样做:

gunicorn  ecommerce.wsgi:application --bind 0.0.0.0:8000

我仍然得到这个:

[2018-08-21 15:09:27 +0000] [18310] [INFO] Starting gunicorn 19.9.0
[2018-08-21 15:09:27 +0000] [18310] [INFO] Listening at: http://0.0.0.0:8000 (18310)
[2018-08-21 15:09:27 +0000] [18310] [INFO] Using worker: sync
[2018-08-21 15:09:27 +0000] [18314] [INFO] Booting worker with pid: 18314
[2018-08-21 15:09:27 +0000] [18314] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/ecommerce/wsgi.py", line 17, in <module>
    from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi

有什么建议吗?

如果我这样做:

pip list

我看到了:

gunicorn (19.9.0)
http (0.02)
pip (1.4.1)
setuptools (0.9.8)
wsgiref (0.1.2)

我可以尝试添加到PYTHONPATH:

 --pythonpath /usr/share/lynette-ecomerce-demo/ecommerce-site/version1/lib/python3.7/site-packages

因此完整的命令是:

gunicorn  ecommerce.wsgi:application --bind 0.0.0.0:8000  --pythonpath /usr/share/lynette-ecomerce-demo/ecommerce-site/version1/lib/python3.7/site-packages

当我尝试得到时:

[2018-08-21 16:26:27 +0000] [18604] [INFO] Starting gunicorn 19.9.0
[2018-08-21 16:26:27 +0000] [18604] [INFO] Listening at: http://0.0.0.0:8000 (18604)
[2018-08-21 16:26:27 +0000] [18604] [INFO] Using worker: sync
[2018-08-21 16:26:27 +0000] [18608] [INFO] Booting worker with pid: 18608
[2018-08-21 16:26:27 +0000] [18608] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/ecommerce/wsgi.py", line 17, in <module>
    from django.core.wsgi import get_wsgi_application
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/lib/python3.7/site-packages/django/__init__.py", line 1, in <module>
    from django.utils.version import get_version
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/lib/python3.7/site-packages/django/utils/version.py", line 71, in <module>
    @functools.lru_cache()
AttributeError: 'module' object has no attribute 'lru_cache'
[2018-08-21 16:26:27 +0000] [18608] [INFO] Worker exiting (pid: 18608)
[2018-08-21 16:26:27 +0000] [18604] [INFO] Shutting down: Master
[2018-08-21 16:26:27 +0000] [18604] [INFO] Reason: Worker failed to boot.                            

如果我这样做:

 find . -name site-packages

我知道:

./django/lib/python2.7/site-packages
./django/lib64/python2.7/site-packages
./lib/python3.7/site-packages

0 个答案:

没有答案