我正在尝试为个人项目配置Django测试应用程序。我之前有Django的经验,但这完全是Python 2.7.x.我想在可能的情况下开始使用3.x,这个项目似乎是一个很好的开始。
Backend是一个标准的Postgres 9.4安装,Apache 2.4,Python 3.4在virtualenvwrapper中,操作系统是OSX 10.10。 mod_wsgi和mod_wsgi3都是通过homebrew
安装的。
然而,有一些问题让mod_wsgi和mod_wsgi3与python 3一起工作。具体来说,./manage.py runserver 8080
工作正常,但当我尝试使用Apache的虚拟主机配置时,我得到了
mod_wsgi (pid=29906): Target WSGI script '/Users/jason/projects/geocode_django/geodjango/geodjango/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=29906): Exception occurred processing WSGI script '/Users/jason/projects/geocode_django/geodjango/geodjango/wsgi.py'.
Traceback (most recent call last):
File "/Users/jason/projects/geocode_django/geodjango/geodjango/wsgi.py", line 28, in <module>
application = get_wsgi_application()
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/apps/config.py", line 197, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/contrib/auth/models.py", line 40, in <module>
class Permission(models.Model):
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/models/base.py", line 125, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/models/base.py", line 300, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/models/options.py", line 166, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/__init__.py", line 40, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/utils.py", line 242, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/utils.py", line 108, in load_backend
return import_module('%s.base' % backend_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 2, in <module>
from django.db.backends.postgresql_psycopg2.base import DatabaseWrapper as Psycopg2DatabaseWrapper
File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 27, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/ site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _PyBytes_Type
Referenced from: /Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/psycopg2/_psycopg.so
Expected in: flat namespace
in /Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/psycopg2/_psycopg.so
我的wsgi.py文件是
import os, sys, site
site.addsitedir("/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages")
sys.path.append("/Users/jason/projects/geocode_django/geodjango")
sys.path.append("/Users/jason/projects/geocode_django/geodjango/geodjango")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "geodjango.settings")
with open("/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py") as f:
code = compile(f.read(), "/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py", "exec")
exec(code, dict(__file__="/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py"))
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
和virtualhost文件是:
LoadModule wsgi_module /usr/local/Cellar/mod_wsgi3/3.5/libexec/mod_wsgi.so
<VirtualHost *:80>
ServerName dev.geocode.com
ServerAlias geocode.com
ServerAdmin jason@someschool.edu
DocumentRoot "/Users/jason/projects/geocode_django"
Alias /static/ /Users/jason/projects/geocode_django/static/
WSGIScriptAlias / /Users/jason/projects/geocode_django/geodjango/geodjango/wsgi.py
<Directory /Users/jason/projects/geocode_django/static >
require all granted
</Directory>
<Directory /Users/jason/projects/geocode_django/geodjango/geodjango >
<Files wsgi.py >
Require all granted
</Files>
</Directory>
CustomLog /Users/jason/projects/geocode_django/logs/access.log combined
ErrorLog /Users/jason/projects/geocode_django/logs/error.log
</VirtualHost>
我的问题是,如果我有一个python 2.7 virtualenv,同一个虚拟主机,LoadModule
指向
LoadModule wsgi_module /usr/local/Cellar/mod_wsgi/4.4.7/libexec/mod_wsgi.so
并替换
with open("/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py") as f:
code = compile(f.read(), "/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py", "exec")
exec(code, dict(__file__="/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py"))
在wsgi.py中
exec(open("/Users/jasonjohns/.virtualenvs/geocode_env/bin/activate_this.py").read())
我可以使用网址dev.geocode.com
加载网站。否则,我会在日志文件中收到错误输出错误500页。
我原本以为这是psycopg2的问题,并提交了bug report。但是,开发人员关闭的问题是关注mod_wsgi而不是psycopg。
没有为我的本地环境编译mod_wsgi,有没有办法解决这个问题?
答案 0 :(得分:3)
通过mod_wsgi google小组提供Graham Dumpleton的提示,找到了解决方案。核心问题是mod_wsgi
和brew install mod_wsgi
安装的pip install mod_wsgi
版本都是针对Python 2.7.x编译的。
另一件事。在pip
返回python --version
的系统上,Python 2.7.6
将使用该Python版本来构建任何安装命令。如果您有Python 3.x,并且2.7.x和python --version
返回Python 3.x.x
,pip3 install ...
将下载3.x兼容代码并为该Python版本构建。
解决方案是通过
删除两个安装brew uninstall mod_wsgi
pip uninstall mod_wsgi
并通过
安装pip3 install mod_wsgi
因此,Apache配置文件的LoadModule位置已更改为
/usr/local/lib/python3.4/site-packages/mod_wsgi-4.4.9-py3.4-macosx-10.10-x86_64.egg/mod_wsgi/server/mod_wsgi-py34.so
请注意路径中的python3.4
位置。
进行这些更改并重新启动Apache后,网站会按预期加载。