首页 > 解决方案 > 如何设置 appache2 的 WSGI 以使用 python 3.7?

问题描述

我正在使用 ubuntu 16.04 并安装了 python 3.7 并使用以下说明将其设置为默认值: Unable to set default python version to python3 in ubuntu when I type pythonin the console 我得到 python3.7 ,我试图将 appache2 设置为使用 python 3.7使用 :

sudo add-apt-repository --yes ppa:deadsnakes/ppa
sudo apt-get update --yes
sudo apt-get install --yes python3.7
sudo apt-get install --yes python3-pip
sudo apt-get --yes install python3-pip apache2 libapache2-mod-wsgi-py3 
sudo a2enmod wsgi
sudo apt install --yes python-django-common
sudo apt-get  install --yes python-django

但是当我尝试访问我在终端上没有得到的服务器时,我仍然会遇到已经安装在 /var/log/apache2/error.log 中的导入包的例外情况:

Traceback (most recent call last):
 File "/home/ubuntu/my_code/wsgi.py", line 11, in <module>
     from django.core.wsgi import get_wsgi_application
 ImportError: No module named 'django'
 mod_wsgi (pid=75005): Target WSGI script '/home/ubuntu/my_code/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=75005): Exception occurred processing WSGI script '/home/ubuntu/my_code/wsgi.py'.
 Traceback (most recent call last):
  File "/home/ubuntu/my_code/wsgi.py", line 11, in <module>
  from django.core.wsgi import get_wsgi_application

mod_wsgi (pid=75005): Target WSGI script '/home/ubuntu/my_code/wsgi.py' cannot be loaded as Python module.

即使我在 python 3.7 中安装了 django,我得到的另一个错误是在服务重启后:

 mod_wsgi (pid=89300): Call to 'site.addsitedir()' failed for '(null)', stopping.

我的 wsgiy.py :

import os
import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
path='/home/ubuntu/my_code/'

if path not in sys.path:
  sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'my_code.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

这个错误的原因是什么?

标签: pythonpython-3.xdjangoubuntupython-3.7

解决方案


您需要构建 python 然后构建 wsgi,因为默认 wsgi 是基于 is system python 构建的,请使用本教程 https://medium.com/@garethbjohnson/serve-python-3-7-with-mod-wsgi-on -ubuntu-16-d9c7ab79e03a


推荐阅读