Django,VirtualEnv,Ubuntu,uwsgi / gunicorn:无法找到所有python / django模块

时间:2013-09-13 19:57:05

标签: python django ubuntu uwsgi pythonpath

我正在尝试设置uWSGI来为我的django应用程序提供服务,但我遇到了路径问题,或者至少我认为这是一个路径问题。症状是当我向应用程序发出curl请求时,我得到回溯,表明找不到基本的python / django模块。例如:

File "/opt/example/venvs/example/lib/python2.7/site-packages/django/test/_doctest.py", line 104, in <module> import unittest, difflib, pdb, tempfile
File "/usr/lib/python2.7/pdb.py", line 59, in <module>
class Pdb(bdb.Bdb, cmd.Cmd): AttributeError: 'module' object has no attribute 'Cmd'

我看过其他与SO相关的问题,但似乎没有一个问题。

这是我的设置:

  • 的django == 1.5.1
  • uwsgi == 1.9.15(从pip安装)
  • python是2.7
  • Ubuntu 12.04 LTS
  • nginx = 0.8.54(但我不相信这是相关的,因为我还没有将uwsgi连接到nginx)

我正在一个流浪汉创建的虚拟盒中运行,厨师和工厂正在进行所有典型的配置。

这是我进入机器后虚拟机的路径:

PATH="/opt/example/venvs/example/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/vagrant_ruby/bin"

这是.wsgi文件(除了我为调试添加的打印路径外,相当标准):

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings")

import sys
print sys.path

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

这是uwsgi ini文件(注意我必须添加的所有各种python路径):

[uwsgi]
#socket=/tmp/hs.com.sock
virtualenv=/opt/example/venv
chdir=/opt/example/apps/example/src
module=example.wsgi:application
master=true
workers=8
pidfile=/opt/example/apps/example/src/example/uwsgi-master.pid
max-requests=5000
#plugins=python
enable-threads=true
processes=2
no-site=true
http=:8003
#env=DJANGO_SETTINGS_MODULE=example.settings
pythonpath=..
pythonpath=/vagrant/src
pythonpath=/opt/example/apps/example/src/example
pythonpath=/opt/example/venvs/example/lib/python2.7
pythonpath=/opt/example/venvs/example/lib/python2.7/plat-linux2
pythonpath=/opt/example/venvs/example/lib/python2.7/lib-tk
pythonpath=/opt/example/venvs/example/lib/python2.7/lib-old
pythonpath=/opt/example/venvs/example/lib/python2.7/lib-dynload
pythonpath=/opt/example/venvs/example/lib/python2.7/distutils
#pythonpath=/opt/example/venvs/example/lib/python2.7/encodings
#pythonpath=/opt/example/venvs/example/lib/python2.7/config
pythonpath=/usr/lib/python2.7/
pythonpath=/usr/lib/python2.7/lib-dynload/
pythonpath=/usr/lib/python2.7/distutils/
pythonpath=/usr/lib/python2.7/plat-linux2/
pythonpath=/usr/lib/python2.7/lib-tk/
#pythonpath=/opt/example/venvs/example/lib/python2.7/site-packages/django/
#pythonpath=/opt/example/venvs/example/lib/python2.7/site-packages/django/core/
pythonpath=/opt/example/venvs/example/lib/python2.7/site-packages/django/core/management
#pythonpath=/opt/example/venvs/example/lib/python2.7/site-packages/django/core/management/commands
pythonpath=/opt/example/venvs/example/local/lib/python2.7/site-packages
pythonpath=/opt/example/venvs/example/lib/python2.7
pythonpath=/opt/example/venvs/example/lib/python2.7/site-packages
pythonpath=/vagrant/src/example

那里有如此多的python路径的原因是因为应用程序未能找到非常基本的模块,因此我不断添加路径以使其解决。这可以解决几个错误,但1)我找不到上面的Cmd attributerror的正确路径,2)添加这些单独的路径只是没有FEEL正确。

uwsgi输出到'uwsgi --ini inifile'

[uWSGI] getting INI configuration from xxx
*** Starting uWSGI 1.9.15 (32bit) on [Fri Sep 13 18:12:44 2013] ***
compiled with version: 4.6.3 on 13 September 2013 06:46:44
os: Linux-3.2.0-23-generic-pae #36-Ubuntu SMP Tue Apr 10 22:19:09 UTC 2012
nodename: xxx
machine: i686
clock source: unix
detected number of CPU cores: 1
current working directory: /vagrant/src
writing pidfile to /opt/example/apps/example/src/example/uwsgi-master.pid
detected binary path: /opt/example/venvs/example/bin/uwsgi
your processes number limit is 1824
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
uWSGI http bound on localhost:8003 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:34622 (port auto-assigned) fd 3
Python version: 2.7.3 (default, Apr 10 2013, 06:03:17)  [GCC 4.6.3]
Set PythonHome to /opt/example/venv
Python main interpreter initialized at 0xa044e58
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 192012 bytes (187 KB) for 2 cores
*** Operational MODE: preforking ***
added ../ to pythonpath.
added /vagrant/src/ to pythonpath.
added /opt/example/apps/example/src/example/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/plat-linux2 to pythonpath.
added /opt/example/venvs/example/lib/python2.7/lib-tk to pythonpath.
added /opt/example/venvs/example/lib/python2.7/lib-old to pythonpath.
added /opt/example/venvs/example/lib/python2.7/lib-dynload/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/distutils/ to pythonpath.
added /usr/lib/python2.7/ to pythonpath.
added /usr/lib/python2.7/lib-dynload/ to pythonpath.
added /usr/lib/python2.7/distutils/ to pythonpath.
added /usr/lib/python2.7/plat-linux2/ to pythonpath.
added /usr/lib/python2.7/lib-tk/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/site-packages/django/core/management/ to pythonpath.
added /opt/example/venvs/example/local/lib/python2.7/site-packages/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/site-packages/ to pythonpath.
added /vagrant/src/example/ to pythonpath.
['/vagrant/src/example/', '/opt/example/venvs/example/lib/python2.7/site-packages/', '/opt/example/venvs/example/lib/python2.7/', '/opt/example/venvs/example/local/lib/python2.7/site-packages/', '/opt/example/venvs/example/lib/python2.7/site-packages/django/core/management/', '/usr/lib/python2.7/lib-tk/', '/usr/lib/python2.7/plat-linux2/', '/usr/lib/python2.7/distutils/', '/usr/lib/python2.7/lib-dynload/', '/usr/lib/python2.7/', '/opt/example/venvs/example/lib/python2.7/distutils/', '/opt/example/venvs/example/lib/python2.7/lib-dynload/', '/opt/example/venvs/example/lib/python2.7/lib-old', '/opt/example/venvs/example/lib/python2.7/lib-tk', '/opt/example/venvs/example/lib/python2.7/plat-linux2', '/opt/example/venvs/example/lib/python2.7/', '/opt/example/apps/example/src/example/', '/vagrant/src/', '../', '.', '', '/opt/example/venv/lib/python2.7/', '/opt/example/venv/lib/python2.7/plat-linux2', '/opt/example/venv/lib/python2.7/lib-tk', '/opt/example/venv/lib/python2.7/lib-old', '/opt/example/venv/lib/python2.7/lib-dynload', '/vagrant/src/example']
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0xa044e58 pid: 2911 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 2911)
spawned uWSGI worker 1 (pid: 2912, cores: 1)
spawned uWSGI worker 2 (pid: 2913, cores: 1)
spawned uWSGI http 1 (pid: 2914)

从另一个终端我运行curl -v localhost:8003,我得到了'AttributeErrror ...',我在上面发布了。卷曲输出是:

curl: (52) Empty reply from server
* Closing connection #0

这是我尝试过的:

  1. 使用最新的uWSGI版本1.9.15和LTS版本1.4.x
  2. 在virtualenv及其外部安装uWSGI
  3. django的runserver服务我的应用程序很好,我可以看到由于wsgi文件中的调试打印sys.path语句打印出来的路径,它只是我在wsgi.ini文件中的路径的一个子集
  4. 还从wsgi.py文件中打印出runserver和uwsgi环境中的环境变量,我没有看到任何不同的重要内容
  5. uwsgi能够毫无问题地提供基本的test.py应用程序文件,因此我相信可以消除任何安装/设置问题
  6. 尝试使用sudo运行uwsgi
  7. 我还尝试使用基本的django应用程序(直接从startproject创建),并且这个ALSO FAILS具有相同的属性错误,并且需要相同的pythonpath列表。

    我一定是缺少一些简单的设置或配置......?如果需要更多信息,请与我们联系。

    Edit1:这看起来并不特定于uWSGI,但更多与virtualenv设置和路径有关。我安装了gunicorn并得到了完全相同的结果。

1 个答案:

答案 0 :(得分:0)

我认为您的uwsgi.ini文件在

上缺少's'
virtualenv=/opt/example/venv