我让mod_wsgi在RHEL / Apache2.4 / python3.4 / Django1.11 / virtualenv上正常工作。然后,我在virtualenv中使用pip将numpy从1.14.0升级到1.14.2,安装了pandas,并安装了其他几个模块。然后,我重新启动Apache,并开始收到指示AttributeError: 'module' object has no attribute 'arange' when trying to call numpy.arange
的服务器错误,或尝试调用matplotlib.use('Agg')等时出现类似错误。
我将wsgi.py替换为以下内容,并且工作正常。一旦取消注释,任何numpy或matplotlib调用都会返回500错误,并且在apache错误日志中会显示类似于AttributeError: 'module' object has no attribute ...
import datetime
import sys
import numpy as np
import matplotlib as mpl
import mod_wsgi
def application(env, start_response):
#mpl.use('Agg')
#a = np.arange(15).reshape(3, 5)
#a = np.array([2,3,4])
status = '200 OK'
output = 'Hello World! This is the wsgi app generated from python!'
output += '\n\ncurrent time is: '+str(datetime.datetime.now())
output += '\n\nsys.executable='+sys.executable
output += '\n\nsys.path='+str(sys.path)
output += '\n\nsys.version=' + str(sys.version)
output += '\n\nsys.prefix=' + str(sys.prefix)
output += '\n\nmod_wsgi.version='+str(mod_wsgi.version)
output += '\n\n\nEnvironment Variables:\n\n'+'\n'.join('%s: %s' % (k, v) for (k, v) in env.items())
output = bytes(output,'utf-8')
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
此wsgi.py的相关输出:
sys.executable=/usr/bin/python3
sys.path=['/usr/local/virtualenvs/myapp/wsgiapp', '/usr/local/virtualenvs/myapp/lib64/python34.zip', '/usr/local/virtualenvs/myapp/lib64/python3.4', '/usr/local/virtualenvs/myapp/lib64/python3.4/plat-linux', '/usr/local/virtualenvs/myapp/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4', '/usr/lib/python3.4', '/usr/local/virtualenvs/myapp/lib/python3.4/site-packages']
sys.version=3.4.8 (default, Mar 23 2018, 10:04:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
sys.prefix=/usr/local/virtualenvs/myapp
mod_wsgi.version=(4, 5, 24)
mod_wsgi.process_group: mywsgiapp
mod_wsgi.application_group:
Apache httpd.conf不变。相关行:
LoadModule wsgi_module "/usr/local/virtualenvs/myapp/lib64/python3.4/site-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so"
WSGIDaemonProcess mywsgiapp python-home=/usr/local/virtualenvs/myapp python-path=/usr/local/virtualenvs/myapp/wsgiapp
WSGIProcessGroup mywsgiapp
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias /myapp /usr/local/virtualenvs/myapp/wsgiapp/wsgi.py process-group=mywsgiapp application-group=%{GLOBAL}
我不确定是什么原因导致此中断。我知道这通常在为错误版本的python编译mod_wsgi时发生。但这之前运行良好,并且一切似乎仍然配置正确:mod_wsgi正常运行,wsgi.py的输出表明它使用的是正确版本的python,virtualenv的site-packages目录位于python-路径。
我在python3.4的主要位置安装了mod_wsgi,并尝试将Apache LoadModule指向该实例,但仍然遇到相同的错误。
我还应该检查什么?
sys.executable应该指向virtualenv而不是主python安装吗?我应该pip uninstall
所有模块并重新开始吗?
另外,要安装熊猫,我必须安装Cython,这又需要安装gcc-c ++(sudo yum install gcc-c++
)。这会影响什么吗?
已编辑以添加错误跟踪。我取消了上述wsgi.py文件中的a = np.arange(15).reshape(3, 5)
行的注释。
Apache的HTTP错误页面:
服务器遇到内部错误或 配置错误,无法完成 您的请求。
请通过以下方式与服务器管理员联系: root @ localhost通知他们该错误发生的时间, 以及您在此错误之前执行的操作。
可能会提供有关此错误的更多信息 在服务器错误日志中。
Apache错误日志(请注意,在此处发布之前,我先将IP地址删除):
[Thu Jul 12 23:01:15.664941 2018] [wsgi:error] [pid 21490] [remote 10.x.x.x:59961] mod_wsgi (pid=21490): Exception occurred processing WSGI script '/usr/local/virtualenvs/myapp/wsgiapp/wsgi.py'.
[Thu Jul 12 23:01:15.727450 2018] [wsgi:error] [pid 21490] [remote 10.x.x.x:59961] Traceback (most recent call last):
[Thu Jul 12 23:01:15.727814 2018] [wsgi:error] [pid 21490] [remote 10.x.x.x:59961] File "/usr/local/virtualenvs/myapp/wsgiapp/wsgi.py", line 14, in application
[Thu Jul 12 23:01:15.727831 2018] [wsgi:error] [pid 21490] [remote 10.x.x.x:59961] a = np.arange(15).reshape(3, 5)
[Thu Jul 12 23:01:15.727865 2018] [wsgi:error] [pid 21490] [remote 10.x.x.x:59961] AttributeError: 'module' object has no attribute 'arange'
尝试显示np时出错。文件:
[Thu Jul 12 23:25:59.196534 2018] [wsgi:error] [pid 22747] [remote 10.x.x.x:60353] mod_wsgi (pid=22747): Exception occurred processing WSGI script '/usr/local/virtualenvs/myapp/wsgiapp/wsgi.py'.
[Thu Jul 12 23:25:59.257214 2018] [wsgi:error] [pid 22747] [remote 10.x.x.x:60353] Traceback (most recent call last):
[Thu Jul 12 23:25:59.257500 2018] [wsgi:error] [pid 22747] [remote 10.x.x:60353] File "/usr/local/virtualenvs/myapp/wsgiapp/wsgi.py", line 23, in application
[Thu Jul 12 23:25:59.257535 2018] [wsgi:error] [pid 22747] [remote 10.x.x.x:60353] output += '\\n\\nnumpy file location='+str(np.__file__)
[Thu Jul 12 23:25:59.257569 2018] [wsgi:error] [pid 22747] [remote 10.x.x.x:60353] AttributeError: 'module' object has no attribute '__file__'
此外,仅供参考,如果我激活virtualenv并使用解释器,则导入模块的效果很好:
Python 3.4.8 (default, Mar 23 2018, 10:04:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__file__
'/usr/local/virtualenvs/myapp/lib/python3.4/site-packages/numpy/__init__.py'
从wsgi.py文件中打印dir(np)会导致:
dir(np)=['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
因此它不会出错,但是它不是标识符的完整列表。从virtualenv解释器运行dir(np)会导致:
['ALLOW_THREADS', 'AxisError', 'BUFSIZE', 'CLIP', 'ComplexWarning', 'DataSource', 'ERR_CALL', ..... clipped for brevity - there's a full screen of items in the list
因为它确实列出了一些标识符,所以这是从wsgi.py中打印每个标识符的结果。看来确实是从正确的路径拿起包裹。
np.__path__ =_NamespacePath(['/usr/local/virtualenvs/myapp/lib/python3.4/site-packages/numpy'])
np.doc=None
np.loader=<_frozen_importlib._NamespaceLoader object at 0x7f83b82e0c88>
np.package=numpy
np.name=numpy
np.Spec=ModuleSpec(name='numpy', loader=None, origin='namespace', submodule_search_locations=_NamespacePath(['/usr/local/virtualenvs/myapp/lib/python3.4/site-packages/numpy']))
请确保wsgi / python确实找到了numpy软件包,然后我将wsgi.py文件更改为导入不存在的软件包import foo as np
,然后再次运行它时,错误提示为{{1 }}。因此,它似乎实际上是在查找站点软件包...但只是没有完全加载它们?
答案 0 :(得分:0)
有趣的事实:pip安装新模块时,它会设置权限,以便用户能够按预期导入和使用模块,但是“其他”权限允许看到模块但不能执行。因此,在Apache下运行的mod_wsgi能够导入模块,但是调用任何方法都会导致错误。
解决方案:在安装或更新python模块后仔细检查权限!我将所有目录设置为755,并将所有文件设置为644,这似乎可以正常工作。或者,如注释中所述,请确保在运行pip之前已正确设置了umask。