简体   繁体   English

Django,Python 3和Postgres - mod_wsgi问题

[英]Django, Python 3 & Postgres - mod_wsgi issue

I'm trying to configure a Django test application for a personal project. 我正在尝试为个人项目配置Django测试应用程序。 I have previous Django experience, but that was all with Python 2.7.x. 我之前有Django的经验,但这完全是Python 2.7.x. I'd like to start using 3.x when possible, and this project seems like a good way to start. 我想尽可能开始使用3.x,这个项目似乎是一个很好的开始。

Backend is a standard Postgres 9.4 installation, Apache 2.4, Python 3.4 in a virtualenvwrapper, operating system is OSX 10.10. Backend是一个标准的Postgres 9.4安装,Apache 2.4,Python 3.4在virtualenvwrapper中,操作系统是OSX 10.10。 mod_wsgi and mod_wsgi3 have both been installed via homebrew . mod_wsgi和mod_wsgi3都是通过homebrew安装的。

However, there are issues getting mod_wsgi and mod_wsgi3 to work with python 3. Specifically, ./manage.py runserver 8080 works fine, but when I try to use a virtualhost configuration with Apache, I get 但是,有问题让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

My wsgi.py file is 我的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()

and virtualhost file is: 和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>

My issue is that if I have a python 2.7 virtualenv with the same virtualhost with the LoadModule pointing to 我的问题是,如果我有一个python 2.7 virtualenv与相同的虚拟主机与LoadModule指向

LoadModule wsgi_module /usr/local/Cellar/mod_wsgi/4.4.7/libexec/mod_wsgi.so

and replace 并替换

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"))

in wsgi.py with 在wsgi.py中

exec(open("/Users/jasonjohns/.virtualenvs/geocode_env/bin/activate_this.py").read())

I can load up the site with the URL dev.geocode.com . 我可以使用URL dev.geocode.com加载该站点。 Otherwise, I get an error 500 page with the error output in the log file. 否则,我在日志文件中收到错误输出错误500页。

I originally thought this was an issue with psycopg2, and filed a bug report . 我原本以为这是psycopg2的一个问题,并提交了一个错误报告 However, the dev closed the issue as being concerned with mod_wsgi, not psycopg. 但是,开发人员关注mod_wsgi,而不是psycopg。

Short of compiling mod_wsgi for my local environment, is there a way to fix this issue? 没有为我的本地环境编译mod_wsgi,有没有办法解决这个问题?

With a hint from Graham Dumpleton via the mod_wsgi google group, a solution was found. 通过mod_wsgi google组从Graham Dumpleton获得提示,找到了解决方案。 The core issue was that the version of mod_wsgi installed by both brew install mod_wsgi and pip install mod_wsgi were all compiled for Python 2.7.x. 核心问题是,版本mod_wsgi通过同时安装brew install mod_wsgipip install mod_wsgi全部编译为Python 2.7.x.

Another thing. 另一件事。 pip , on a system where python --version returns Python 2.7.6 , will use that Python version for building any install commands. pip ,在python --version返回Python 2.7.6的系统上,将使用该Python版本来构建任何安装命令。 If you have Python 3.x, alongside 2.7.x and python --version returns Python 3.xx , pip3 install ... will download 3.x compatible code and build for that Python version. 如果你有Python 3.x,并且2.7.x和python --version返回Python 3.xxpip3 install ...将下载3.x兼容代码并为该Python版本构建。

The solution was to remove both installations via 解决方案是通过删除两个安装

brew uninstall mod_wsgi
pip uninstall mod_wsgi

and install via 并安装通过

pip3 install mod_wsgi

As a result, the LoadModule location for the Apache configuration file was changed to 因此,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

Note the python3.4 location in the path. 注意路径中的python3.4位置。

After making these changes and restarting Apache, the site loads as expected. 进行这些更改并重新启动Apache后,该站点将按预期加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM