uWSGI + Django + Virtualenv 无法加载 _functools(导入错误)

2 投票
4 回答
6031 浏览
提问于 2025-04-16 05:50

好的,我试过在有和没有虚拟环境的情况下运行这个:

uwsgi --home /home/auston/new_proj/ --socket /tmp/uwsgi2.sock --chmod-socket --module app_wsgi --pp /home/auston/new_proj/nikeshere --logto /tmp/uwsgi.log --master --processes 4 -P

无论怎么做,我几乎总是得到这个结果:

*** Starting uWSGI 0.9.6.5 (32bit) on [Thu Oct 21 08:05:44 2010] ***
compiled with version: 4.4.3
Python version: 2.6.6 (r266:84292, Oct 21 2010, 04:07:38)
[GCC 4.4.3]
your memory page size is 4096 bytes
allocated 412 bytes (0 KB) for 1 request's buffer.
Setting PythonHome to /home/auston/new_proj/...
binding on UNIX socket: /tmp/uwsgi2.sock
chmod() socket to 666 for lazy and brave users
your server socket listen backlog is limited to 64 connections
added /home/auston/new_proj/nikeshere to pythonpath.
initializing hooks...done.
['/home/auston/new_proj/nikeshere', '.', '', '/home/auston/new_proj/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg', '/home/auston/new_proj/lib/python2.6/site-packages/pip-0.8.1-py2.6.egg', '/home/auston/new_proj/lib/python26.zip', '/home/auston/new_proj/lib/python2.6', '/home/auston/new_proj/lib/python2.6/plat-linux2', '/home/auston/new_proj/lib/python2.6/lib-tk', '/home/auston/new_proj/lib/python2.6/lib-old', '/home/auston/new_proj/lib/python2.6/lib-dynload', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/home/auston/new_proj/lib/python2.6/site-packages', '/usr/local/lib/python2.6/dist-packages/pip-0.8.1-py2.6.egg', '/usr/local/lib/python2.6/site-packages', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/home/auston/new_proj/nikeshere', '/usr/local/lib/python2.6']
Traceback (most recent call last):
  File "/home/auston/new_proj/nikeshere/app_wsgi.py", line 11, in <module>
    import django.core.handlers.wsgi
  File "/usr/local/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 1, in <module>
    from threading import Lock
  File "/usr/lib/python2.6/threading.py", line 13, in <module>
    from functools import wraps
  File "/usr/lib/python2.6/functools.py", line 10, in <module>
    from _functools import partial, reduce
ImportError: No module named _functools

如果我把 --home 改成 /usr/local/lib/python/2.6,我在 app_wsgi.py 中导入 os 的时候就会失败。下面是代码,供你参考:

import sys
import os

sys.path.append(os.path.abspath(os.path.dirname(__file__)))

import django.core.handlers.wsgi

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

基本上,我想问的是,如何让 uWSGI 识别 functools,或者如何找到正确的路径(上面的输出中有路径)。如果你们能提供任何帮助,我将非常感激!!

附注:我用的是 Ubuntu 10.04 - uWSGI 0.9.6.5 - NGINX 0.8.53 - 虚拟环境 Python 2.6.5 - "常规(或系统)" Python 2.6.6 - Django 1.2.3

更新:

我发现如果我省略 "--module",uwsgi 就能开始接受请求,如下所示:

uwsgi --home /home/auston/new_proj --socket /tmp/uwsgi2.sock --chmod-socket --pp /home/auston/new_proj/nikeshere --logto /tmp/uwsgi.log --master --processes 4 -P

但现在我遇到了一个应用程序未找到的错误:

"uWSGI 错误
未找到 wsgi 应用程序"

我离解决问题更近了,但我仍然希望能得到一些建议,因为应用程序未找到是因为我无法包含加载它所需的模块!

4 个回答

0

你可以看看这个链接:http://blog.zacharyvoase.com/2010/03/05/django-uwsgi-nginx/。里面的设置和我们用的很相似。

1

我知道这个话题有点老,而且关于堆栈构建块的版本也变了,但我在使用uWSGi时遇到了同样的问题,就是它无法识别在虚拟环境中安装的库。解决办法是把home参数指向虚拟环境,下面的例子就是这样做的(来自https://uwsgi.readthedocs.org/en/latest/tutorials/Django_and_nginx.html)。

所以对我来说,下面这个命令:

uwsgi --http :8000 --module ii.wsgi --home /home/dev/.virtualenvs/ii_env/

在django应用的(ii)目录下运行时是有效的。

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /path/to/your/project
# Django's wsgi file
module          = project.wsgi
# the virtualenv (full path)
home            = /path/to/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
1

正如上面提到的,问题出在pythonpath上,它找不到一个叫做_functools的模块。

显然,_functools是一个C语言写的模块,我需要把它的路径加到pythonpath里,这样才能找到它。所以,现在的wsgi.py和原来的不同之处在于:

import sys
sys.path.append('/usr/local/lib/python2.6/lib-dynload') # to load _functools
sys.path.append('/usr/local/lib/python2.6/site-packages') # to load django
sys.path.append('/usr/local/lib/python2.6/dist-packages') # cautionary to load django
sys.path.append('/usr/lib/python2.6') # to load os
import os

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

import django.core.handlers.wsgi

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

虽然这个方法有点不太正规,但现在能用就行...

撰写回答