Friday, 13 September 2013

uWSGI, Django, VirtualEnv, Ubuntu: Unable to locate all python/django modules

uWSGI, Django, VirtualEnv, Ubuntu: Unable to locate all python/django modules

I'm trying to setup uWSGI to serve my django app but I'm having issues
with the path, or at least I think it's a path issue. The symptom is that
when I make a curl request to the application, I get tracebacks indicating
that basic python/django modules can't be found. ex:
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'
I've looked at the other SO related questions but none appear to be my issue.
Here's my setup:
django==1.5.1
uwsgi==1.9.15 (installed from pip)
python is 2.7
Ubuntu 12.04 LTS
nginx = 0.8.54 ( but I don't believe this is relevant as I haven't
connected uwsgi up to nginx yet )
I'm running in a vagrant created virtualbox, with chef and fab doing all
the typical provisioning.
Here's the path of the VM once I've ssh'd into the machine:
PATH="/opt/example/venvs/example/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/vagrant_ruby/bin"
Here's the .wsgi file ( fairly standard except for the print path that I
added for debug ):
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()
Here's the uwsgi ini file ( notice all the various pythonpaths that I had
to add ):
[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
The reason there are so many pythonpaths in there is because the app was
failing to find very basic modules, and so I kept adding paths to make it
resolve. This worked for several errors, but 1) I can't find the proper
path for the Cmd attributerror above and 2) adding each of these
individual paths just doesn't FEEL right.
the uwsgi output to '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)
From another terminal I run curl -v localhost:8003 and I get the
'AttributeErrror...' i posted above. The curl output is:
curl: (52) Empty reply from server
* Closing connection #0
Here's what I've tried:
using the latest uWSGI version 1.9.15 and the LTS version 1.4.x
installing uWSGI inside the virtualenv and outside of it
django's runserver serves my app just fine, and I can see the path that's
printed out because of the debug print sys.path statement in the wsgi file
and it's simply a subset of the paths that I have in my wsgi.ini file
also printed out environment variables in both runserver and uwsgi
environments from the wsgi.py file and I don't see anything of importance
that are different
uwsgi is able to serve a basic test.py application file without issue, so
I believe that eliminates any installation/setup issues
tried running uwsgi with sudo
I also tried it with a basic django app ( created directly from
startproject ), and this ALSO FAILS with the same attributeerror, and
requires the same list of pythonpaths.
I must be missing some simple setup or configuration... ? Let me know if
more information is required.

No comments:

Post a Comment