熊猫+ Django + mod_wsgi + virtualenv

时间:2011-10-14 10:33:54

标签: django mod-wsgi virtualenv pandas

熊猫正在制作'模块'在虚拟环境中的djangomod_wsgi下导入时,对象没有属性' 。它在虚拟环境中的django开发服务器下运行良好。

其他模块例如:numpy没有问题因此我认为这意味着使用mod_wsgi正确设置了虚拟环境。任何建议将不胜感激。

staging.wsgi

import os
import sys
import site

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
site_packages = os.path.join(PROJECT_ROOT, 'env/openportfolio/lib/python2.7/site-packages')
site.addsitedir(os.path.abspath(site_packages))
sys.path.insert(0, PROJECT_ROOT)
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
os.environ['DJANGO_SETTINGS_MODULE'] = 'openportfolio.settings_staging'

import pandas #triggers error
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

错误

Traceback (most recent call last):
  File "/usr/local/web/django/www/staging/openportfolio/apache/staging.wsgi", line 22, in <module>
    import pandas
  File "/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages/pandas/__init__.py", line 12, in <module>
    from pandas.core.api import *
  File "/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages/pandas/core/api.py", line 6, in <module>
    import pandas.core.datetools as datetools
  AttributeError: 'module' object has no attribute 'core'  

Python Path

['/usr/local/web/django/www/staging/openportfolio',
 '/usr/local/web/django/www/staging',
 '/Library/Python/2.7/site-packages/pip-1.0.2-py2.7.egg',
 '/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
 '/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg',
 '/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages',
 '/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages']

1 个答案:

答案 0 :(得分:1)

事实证明这是一个Python路径顺序问题。通过在我的wsgi配置文件中运行sys.path.reverse(),代码现在可以运行了。

由于Python路径的顺序,必须首先在虚拟环境中导入内置的OS X numpy库,导致问题。

'RuntimeError:针对API版本6编译的模块,但此版本的numpy为4'是我在原始帖子中遗漏的错误行,可能有助于调试答案。