title: mod_wsgi cannot load mysql
body: <p>First I will list my environment and status:</p>
  
  <p>Environment:</p>
  
  <ul>
  <li>System: Mac OS 10.6</li>
  <li>Python: 2.6</li>
  <li>Apache: 2.2</li>
  <li>mod_wsgi: 3.3</li>
  <li>mysql: 5.x</li>
  <li>php: 3.5</li>
  <li>trac:0.12</li>
  </ul>
  
  <p>Status:
  I can run trac as alone-server without problem. 
  Now I integrate trac to apache, problem come out. mod_wsgi can run normal wsgi page correctly, but cannot load mysql connection. I test it in piece of code, put "<strong>import MySQLdb</strong>" in wsgi page, it still cannot. I also can access trac through apache, but it shows me <strong>Cannot load Python bindings for MySQL</strong></p>
  
  <p>I refer to the apache log:</p>
  
  <pre><code>[Sun Mar 13 13:36:44 2011] [error] [client ::1] mod_wsgi (pid=37060): Target WSGI script /Users/alex/Library/apache2/htdocs/sql.wsgi' cannot be loaded as Python module.
  
  [Sun Mar 13 13:36:44 2011] [error] [client ::1] mod_wsgi (pid=37060): Exception occurred processing WSGI script '/Users/alex/Library/apache2/htdocs/sql.wsgi'.
  
  [Sun Mar 13 13:36:44 2011] [error] [client ::1] Traceback (most recent call last):
  
  [Sun Mar 13 13:36:44 2011] [error] [client ::1]   File "/Users/alex/Library/apache2/htdocs/sql.wsgi", line 2, in &lt;module&gt;
  
  [Sun Mar 13 13:36:44 2011] [error] [client ::1]     import MySQLdb
  
  [Sun Mar 13 13:36:44 2011] [error] [client ::1]   File "build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py", line 19, in &lt;module&gt;
  
  [Sun Mar 13 13:36:44 2011] [error] [client ::1]   File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 7, in &lt;module&gt;
  
  [Sun Mar 13 13:36:44 2011] [error] [client ::1]   File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 6, in __bootstrap__
  
  [Sun Mar 13 13:36:44 2011] [error] [client ::1] ImportError: dlopen(/Users/alex/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.16.dylib
  
  [Sun Mar 13 13:36:44 2011] [error] [client ::1]   Referenced from: /Users/alex/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
  
  [Sun Mar 13 13:36:44 2011] [error] [client ::1]   Reason: image not found
  
  [Sun Mar 13 13:36:57 2011] [error] [client ::1] mod_wsgi (pid=37061): Target WSGI script
  </code></pre>
  
  <p>This looks like mod_wsgi cannot find libmysqlclient.16.dylib. I encounter similar problem when I install mysqldb for python, so I add export </p>
  
  <p><code>DYLD_LIBRARY_PATH=/Users/alex/Library/mysql-5.x/lib</code></p>
  
  <p>SO python -c "import MySQLdb" can work, but mod_wsgi cannot work.
  So I put </p>
  
  <pre><code>os.environ['DYLD_LIBRARY_PATH'] = '/Users/alex/Library/mysql-5.x/lib'
  </code></pre>
  
  <p>into trac.ini.
  It doesn't work for trac in apache still. </p>
  
  <p>So anyone has this problem before? 
  Thanks</p>