uWSGI + Django + Virtualenv无法接收_functools(导入错误)

时间:2010-10-21 08:19:45

标签: python django nginx uwsgi

好的,所以我用&没有virtualenv:

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或者走正确的路径(路径在上面的输出中)。我很感激你们给予的任何帮助!!

P.S。 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 :(得分:1)

如上所述,问题出在pythonpath&amp;它无法找到名为_functools的模块。

显然,_functools是一个c模块&amp;我需要将它的路径附加到python路径才能找到它,因此与原始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()

非常hacky,但它现在有用......

答案 1 :(得分:1)

我知道它的旧主题和堆栈构建块的版本已更改,但我遇到了同样的问题,因为未在virtualenv中根据uWSGi安装的库进行识别。解决方案是将home参数指向virtualenv,如下所示(取自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

答案 2 :(得分:0)

结帐http://blog.zacharyvoase.com/2010/03/05/django-uwsgi-nginx/。他正在使用非常相似的设置。

答案 3 :(得分:0)

我遇到了非常类似的问题,我发现了这个:

当你安装virtlenv时,它会通过创建符号链接来安装Python标准库(就像/usr/lib/python2.7一样)。但是当你检查你的virtualenv Python lib目录时,只为几个基本库创建了符号链接。你的functools可能不在其中。

因此解决方案是手动创建符号链接。这是一个PITA,因为你可能需要创建很多符号链接,但它似乎对我来说是一个更清洁的解决方案。您不必破解任何源文件,它是透明的。

符号链接不应该在venv_directory的根目录中创建,而是在例如

中创建。
venv_directory/lib/python2.7/site-packages/

希望它适合你!