当我的virtualenv有python3.5时,为什么django 1.9继续使用python2.7?

时间:2016-09-20 16:54:14

标签: python django apache mod-wsgi centos7

我遇到python版本的问题,我实际上是在Windows 7下使用python3.5开发一个网页。但在我的服务器(CentOS 7)中,我用python3.5创建了一个virtualenv(因为linux中的默认版本是python 2.7)。

问题是,当我收到错误时,它说django正在使用python2.7:

Request Method: GET
Request URL:    http://proyect/url/
Django Version: 1.9.8
Exception Type: UnicodeEncodeError
Exception Value:    
'ascii' codec can't encode character u'\xed' in position 9: ordinal not in range(128)
Exception Location: /usr/lib/python2.7/site-packages/django/utils/encoding.py in force_text, line 80
Python Executable:  /usr/bin/python
Python Version: 2.7.5
Python Path:    
['/home/user/proyect',
 '/home/user/proyect_env/lib/python3.5/site-packages',
 '/usr/lib64/python27.zip',
 '/usr/lib64/python2.7',
 '/usr/lib64/python2.7/plat-linux2',
 '/usr/lib64/python2.7/lib-tk',
 '/usr/lib64/python2.7/lib-old',
 '/usr/lib64/python2.7/lib-dynload',
 '/usr/lib64/python2.7/site-packages',
 '/usr/lib64/python2.7/site-packages/gtk-2.0',
 '/usr/lib/python2.7/site-packages']

我几乎100%确定显示此消息是因为django使用了错误的python版本。

在我的django.conf里面/etc/httpd/conf.d/我配置了这个:

WSGIDaemonProcess proyect python-path=/home/user/proyect:/home/user/proyect_env/lib/python3.5/site-packages
WSGIProcessGroup project
WSGIScriptAlias / /home/user/proyect/proyect/wsgi.py

我按照this教程配置了我的服务器。

编辑#1 在遵循@Ixer指示后,我在/ etc / httpd / logs / error_log

中得到了此错误回溯
7:49:05.114720 2016] [:error] [pid 14836] [remote 10.105.40.106:49676] mod_wsgi (pid=14836): Exception occurred processing WSGI script '/home/user/proyect/proyect/wsgi.py'.
[Tue Sep 20 17:49:05.114779 2016] [:error] [pid 14836] [remote 10.105.40.106:49676] Traceback (most recent call last):
[Tue Sep 20 17:49:05.114810 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 158, in __call__
[Tue Sep 20 17:49:05.114862 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     self.load_middleware()
[Tue Sep 20 17:49:05.114883 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 51, in load_middleware
[Tue Sep 20 17:49:05.114910 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     mw_class = import_string(middleware_path)
[Tue Sep 20 17:49:05.114926 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/utils/module_loading.py", line 20, in import_string
[Tue Sep 20 17:49:05.114951 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     module = import_module(module_path)
[Tue Sep 20 17:49:05.114966 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
[Tue Sep 20 17:49:05.114991 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     __import__(name)
[Tue Sep 20 17:49:05.115007 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 3, in <module>
[Tue Sep 20 17:49:05.115031 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     from django.contrib.auth.backends import RemoteUserBackend
[Tue Sep 20 17:49:05.115046 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/contrib/auth/backends.py", line 4, in <module>
[Tue Sep 20 17:49:05.115070 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     from django.contrib.auth.models import Permission
[Tue Sep 20 17:49:05.115085 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
[Tue Sep 20 17:49:05.115109 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
[Tue Sep 20 17:49:05.115124 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
[Tue Sep 20 17:49:05.115148 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     class AbstractBaseUser(models.Model):
[Tue Sep 20 17:49:05.115163 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 94, in __new__
[Tue Sep 20 17:49:05.115187 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     app_config = apps.get_containing_app_config(module)
[Tue Sep 20 17:49:05.115203 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
[Tue Sep 20 17:49:05.115226 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     self.check_apps_ready()
[Tue Sep 20 17:49:05.115241 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
[Tue Sep 20 17:49:05.115263 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     raise AppRegistryNotReady("Apps aren't loaded yet.")

编辑#2

问题解决了,情况是当我通过pip install -r requirements.txt安装应用程序时,出于某种原因,它们安装在python 2.7下(即使我采购了virtualenv activate)。所以我做的是通过/ home / user / project_env / bin / pip install -r requirements再次安装应用程序...这修复了错误,但现在我遇到了应用程序的问题:

[Tue Sep 20 21:34:49.998172 2016] [:error] [pid 18220] [remote 10.105.40.106:172] mod_wsgi (pid=18220): Target WSGI script '/home/user/project/project/wsgi.py' cannot be loaded as Python module.
[Tue Sep 20 21:34:49.998207 2016] [:error] [pid 18220] [remote 10.105.40.106:172] mod_wsgi (pid=18220): Exception occurred processing WSGI script '/home/rortega/smce/smce/wsgi.py'.
[Tue Sep 20 21:34:49.998229 2016] [:error] [pid 18220] [remote 10.105.40.106:172] Traceback (most recent call last):
[Tue Sep 20 21:34:49.998255 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project/project/wsgi.py", line 33, in <module>
[Tue Sep 20 21:34:49.998314 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     application = get_wsgi_application()
[Tue Sep 20 21:34:49.998326 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Tue Sep 20 21:34:49.998362 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     django.setup()
[Tue Sep 20 21:34:49.998372 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
[Tue Sep 20 21:34:49.998406 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     apps.populate(settings.INSTALLED_APPS)
[Tue Sep 20 21:34:49.998417 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
[Tue Sep 20 21:34:49.998516 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     app_config = AppConfig.create(entry)
[Tue Sep 20 21:34:49.998527 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/django/apps/config.py", line 90, in create
[Tue Sep 20 21:34:49.998590 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     module = import_module(entry)
[Tue Sep 20 21:34:49.998601 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
[Tue Sep 20 21:34:49.998637 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     __import__(name)
[Tue Sep 20 21:34:49.998647 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/stdimage/__init__.py", line 5, in <module>
[Tue Sep 20 21:34:49.998676 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     from .models import StdImageField  # NOQA
[Tue Sep 20 21:34:49.998686 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/stdimage/models.py", line 14, in <module>
[Tue Sep 20 21:34:49.998751 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     from PIL import Image, ImageOps
[Tue Sep 20 21:34:49.998761 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/PIL/Image.py", line 67, in <module>
[Tue Sep 20 21:34:49.999163 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     from PIL import _imaging as core
[Tue Sep 20 21:34:49.999185 2016] [:error] [pid 18220] [remote 10.105.40.106:172] ImportError: cannot import name _imaging

2 个答案:

答案 0 :(得分:2)

您需要从Python 3的源代码重建 mod_wsgi

下载 mod_wsgi 。打开包装。

tar xvfz mod_wsgi-X.Y.tar.gz

配置Python 3.5:

./configure --with-apxs=/usr/local/apache/bin/apxs \
  --with-python=/path/to/python3.5

制作并安装:

make
make install

但建议使用Nginx / Uwsgi或Nginx / Gunicorn。 python2和python3都很容易配置。

答案 1 :(得分:-1)

该手册似乎不对,因为它没有说明如何在服务器上使用virtualenv。即使apache配置的最后一行指向wsgi配置,它也没有解释那里应该包含的内容。

尝试这样的事情:

#/home/user/project/project/wsgi.py

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('~/.virtualenvs/myprojectenv/local/lib/python3.5/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/home/django_projects/MyProject')
sys.path.append('/home/django_projects/MyProject/myproject')

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

# Activate your virtual env
activate_env=os.path.expanduser("~/.virtualenvs/myprojectenv/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()