mod_wsgi cannot load MySQLdb

75 views
Skip to first unread message

Jim

unread,
Aug 22, 2011, 6:37:44 PM8/22/11
to django...@googlegroups.com
Hello folks,

This probably has been discussed many times, but I still can't find any solution yet. Basically, it turns out that python can load MySQLdb just fine, but Apache can't load MySQLdb from the wsgi script.

I am using python2.7 in a virtual environment created by virtualenv. Here is the error info extracted from the Apache error log. To make the lines shorter, I deleted all the time tags in the brackets.

Any ideas how to fix it?

[] mod_wsgi (pid=3136): Target WSGI script '/Users/jianbao/projects/tao.com/mysite/apache/django.wsgi' cannot be loaded as Python module.
[] mod_wsgi (pid=3136): Exception occurred processing WSGI script '/Users/jianbao/projects/tao.com/mysite/apache/django.wsgi'.
[] Traceback (most recent call last):
[]   File "/Users/jianbao/projects/tao.com/mysite/apache/django.wsgi", line 56, in <module>
[]     import MySQLdb
[]   File "/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
[]     import _mysql
[] ImportError: dlopen(/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
[]   Referenced from: /Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/_mysql.so
[]   Reason: image not found

Reinout van Rees

unread,
Aug 24, 2011, 4:05:43 AM8/24/11
to django...@googlegroups.com
On 23-08-11 00:37, Jim wrote:
> This probably has been discussed many times, but I still can't find any
> solution yet. Basically, it turns out that python can load MySQLdb just
> fine, but Apache can't load MySQLdb from the wsgi script.
>

- Does apache use the virtualenv's environment?

- Mod_wsgi: does that use the correct python version? mod_wsgi normally
uses /usr/bin/python (python 2.5 or 3.2 whatever) and you might have
used /usr/bin/python2.7.

- Can the apache process find the mysql libraries? Are the environement
variables different? Try printing "os.environ" from your .wsgi script
for instance.

Just some brainstorming.


Reinout

--
Reinout van Rees http://reinout.vanrees.org/
rei...@vanrees.org http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

Jianbao Tao

unread,
Aug 24, 2011, 11:12:19 AM8/24/11
to django...@googlegroups.com
Thank you, Reinout.

I don't know which version of python mod_wsgi uses. Here is a list of python versions on my machine.

/Library/Frameworks/Python.framework//Versions/2.7/bin/python

/usr/bin/python2.5

/usr/bin/python2.6

/usr/bin/pythonw2.5

/usr/bin/pythonw2.6

/usr/local/bin/python

/usr/local/bin/python-32

/usr/local/bin/python2.7

/usr/local/bin/python2.7-32

/usr/local/bin/python3

/usr/local/bin/python3-32

/usr/local/bin/python3.2

/usr/local/bin/python3.2-32

/usr/local/bin/python3.2m

/usr/local/bin/pythonw

/usr/local/bin/pythonw-32

/usr/local/bin/pythonw2.7

/usr/local/bin/pythonw2.7-32

/usr/local/bin/pythonw3

/usr/local/bin/pythonw3-32

/usr/local/bin/pythonw3.2

/usr/local/bin/pythonw3.2-32


As you can see, there is no /usr/bin/python. And '/usr/local/bin/python --version' gives Python 2.7.2., which is normally what I use.

For your second suggestion, I don't know if apache can access mysql libraries. Can you tell me how to find out? 


On Wed, Aug 24, 2011 at 2:05 AM, Reinout van Rees <rei...@vanrees.org> wrote:
On 23-08-11 00:37, Jim wrote:
This probably has been discussed many times, but I still can't find any
solution yet. Basically, it turns out that python can load MySQLdb just
fine, but Apache can't load MySQLdb from the wsgi script.


- Does apache use the virtualenv's environment? And this is what I got from 'print os.environ' in the .wsgi file.
>$ python django.wsgi 
{'TEXPATH': '/usr/local/texlive/2010/texmf-dist/tex/latex', 'PROMPT_COMMAND': 'echo -ne "\\033]0;${tabtit}\\007"', 'IDL_DIR': '/Applications/itt/idl/idl81', 'LOGNAME': 'jianbao', 'USER': 'jianbao', 'ITT_DIR': '/Applications/itt', 'HOME': '/Users/jianbao', 'PATH': '/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.2/bin:/opt/local/bin:/opt/local/sbin:/Users/jianbao/shlib:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/texbin:/usr/X11/bin:/Users/jianbao/pylib:/usr/local/mysql/bin', 'PS1': '\\n\\e[0;34m$PWD\\e[m\\n>$ ', 'DISPLAY': '/tmp/launch-YVSOek/org.x:0', 'TERM_PROGRAM': 'iTerm.app', 'LANG': 'en_US.UTF-8', 'TERM': 'xterm', 'SHELL': '/bin/bash', 'COLORFGBG': '0;15', 'SHLVL': '1', 'ITERM_SESSION_ID': 'w0t0p0', '_': '/Library/Frameworks/Python.framework/Versions/2.7/bin/python', 'MANPATH': '/opt/local/share/man:', 'ARCHFLAGS': '-arch i386 -arch x86_64', 'SSH_AUTH_SOCK': '/tmp/launch-EXjeyZ/Listeners', 'IDL_STARTUP': '$HOME/idllib/idl_startup.pro', 'DYLD_LIBRARY_PATH': '/usr/local/mysql/lib:', 'Apple_PubSub_Socket_Render': '/tmp/launch-67EotD/Render', 'TMPDIR': '/var/folders/Yb/YbFx+MU1HhWVkmSHSxe4fk+++TI/-Tmp-/', 'LSCOLORS': 'ExFxCxDxBxegedabagacad', 'IDL_DLM_PATH': '<IDL_DEFAULT>:+/Users/jianbao/idllib/DLM/idl_geopack_75:+/Users/jianbao/idllib/spice/icy/lib', 'PYTHONSTARTUP': '/Users/jianbao/pylib/startup.py', 'IDL_PATH': '<IDL_DEFAULT>', 'OLDPWD': '/Users/jianbao/projects/tao.com/djangoSite/bin', 'CLICOLOR': '1', '__CF_USER_TEXT_ENCODING': '0x1F5:0:0', 'PWD': '/Users/jianbao/projects/tao.com/djangoSite/apache', 'tabtit': 'djangoSite', 'COMMAND_MODE': 'unix2003'}

I don't see any clue, though.

Thank you very much.

 

- Mod_wsgi: does that use the correct python version? mod_wsgi normally uses /usr/bin/python (python 2.5 or 3.2 whatever) and you might have used /usr/bin/python2.7.

- Can the apache process find the mysql libraries? Are the environement variables different? Try printing "os.environ" from your .wsgi script for instance.

Just some brainstorming.


Reinout

--
Reinout van Rees                    http://reinout.vanrees.org/
rei...@vanrees.org             http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Jianbao Tao

unread,
Aug 24, 2011, 11:14:11 AM8/24/11
to django...@googlegroups.com
Sorry, I pasted the 'python django.wsgi' output in a wrong place.

Reinout van Rees

unread,
Aug 24, 2011, 12:11:49 PM8/24/11
to django...@googlegroups.com
On 24-08-11 17:12, Jianbao Tao wrote:
> Thank you, Reinout.
>
> I don't know which version of python mod_wsgi uses.

Look in /etc/apache2/mods-enabled/wsgi.load (that's on ubuntu, your
mod_wsgi config file might be somewhere else. On one of my older servers
it contains:

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so-2.5

Which means it is a python2.5 mod_wsgi. In ubuntu's case, as it is in
/usr/lib, it means that it uses the system python2.5.

Your python dirs look like this:

> /usr/local/bin/python
>
> /usr/local/bin/python-32
>
> /usr/local/bin/python2.7

That's all locally compiled by hand. There's no way I can tell you which
one of 'em is used by apache :-) But probably what you'd get when
calling "python2.7" if your wsgi config indicates it is for 2.7.

You got your wsgi script to print the environment


>'PATH':
> '/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.2/bin:/opt/local/bin:/opt/local/sbin:/Users/jianbao/shlib:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/texbin:/usr/X11/bin:/Users/jianbao/pylib:/usr/local/mysql/bin',

Wow! That's a lot of custom stuff in there, including multiple python
paths. To me, it looks like pure luck if it is able to find the correct
mysql in there :-)

But what I was looking for: virtualenv puts the bin directory of the
virtualenv in the path. I don't see anything that looks like a
virtualenv dir in there, unless it is the /Users/jianbao/pylib at the end.

I personally don't know how wsgi+virtualenv works together (I use
buildout normally). so you'll have to ask around for that.

Tom Evans

unread,
Aug 24, 2011, 12:25:59 PM8/24/11
to django...@googlegroups.com

Are mysql, python and python-mysql all compiled correctly for the same
architecture? IE all 32 bit or all 64 bit.

ISTR that this can be a problem on OS X, but I'm not an OS X user...

http://stackoverflow.com/questions/1969222/mysql-python-1-2-3-and-os-x-10-5-64-or-32-bit

Cheers

Tom

Jianbao Tao

unread,
Aug 24, 2011, 8:54:48 PM8/24/11
to django...@googlegroups.com
Thanks, Tom, Reinout.

I think I forgot to mention the configuration of my machine. Here it is.
  • Mac OS Snow Leopard 10.6.8
  • mysql-5.5.14-osx10.6-x86_64
  • Apache2 (shipped with OSX)
  • mod_wsgi 3.3
And here is some additional information that may help.
<shell>
>$ file /usr/libexec/apache2/mod_wsgi.so
/usr/libexec/apache2/mod_wsgi.so: Mach-O universal binary with 2 architectures
/usr/libexec/apache2/mod_wsgi.so (for architecture x86_64): Mach-O 64-bit bundle x86_64
/usr/libexec/apache2/mod_wsgi.so (for architecture i386): Mach-O bundle i386

>$ otool -L /usr/libexec/apache2/mod_wsgi.so
/usr/libexec/apache2/mod_wsgi.so:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)
/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 550.43.0)
</shell>

And I do use virtualenv. Here is how I enable the virtual environment in the .wsgi file.
<code>
"""
Modify sys.path to conform to virtualenv setup.
The basic idea is to add packages installed in the virtual environment to the
front of sys.path so that python will use them instead of those from the
system site-packages.
"""
# Get old sys.path.
old_sys_path = list(sys.path)
virt_site_packages = os.path.join(path, siteName, 'lib/python2.7/site-packages')

site.addsitedir(virt_site_packages)

# Put the added new paths to the front of sys.path.
new_sys_path = []
for item in list(sys.path):
    if item not in old_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)

sys.path[:0] = new_sys_path
</code>

The process is pretty standard, which can be found in the mod_wsgi website.

So, any thoughts?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

Jianbao Tao

unread,
Aug 24, 2011, 8:55:48 PM8/24/11
to django...@googlegroups.com
BTW, the virtualenv uses python2.7
Reply all
Reply to author
Forward
0 new messages