title: Python's MySQLdb can’t find libmysqlclient.dylib with Homebrewed MySQL
body: <h1>MySQL and Python installed with Homebrew</h1>
  <p>I installed MySQL and Python with Homebrew on OS X 10.10.5 Yosemite. My Python 2.7 is at <code> python -&gt; ../Cellar/python/2.7.9/bin/python</code> with a symlink to it at <code>/usr/local/bin/python</code>.</p>
  <p>In <code>/usr/local/bin</code> there is a symlink:<br />
  <code>mysql -&gt; ../Cellar/mysql/5.7.9/bin/mysql</code></p>
  <h2>The error</h2>
  <p>In the Python shell:</p>
  <pre><code>&gt;&gt;&gt; import MySQLdb
  Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
    File &quot;/usr/local/lib/python2.7/site-packages/MySQLdb/__init__.py&quot;, line 19, in &lt;module&gt;
      import _mysql
  ImportError: dlopen(/usr/local/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
    Referenced from: /usr/local/lib/python2.7/site-packages/_mysql.so
    Reason: image not found
  </code></pre>
  <p>So I tried:<br />
  <code>$ sudo unlink /usr/local/lib/libmysqlclient.18.dylib</code></p>
  <p>followed by:<br />
  <code>DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH</code></p>
  <p>and then (desperation over reason):<br />
  <code>$ export DYLD_LIBRARY_PATH=/usr/local/Cellar/mysql/5.7.9/lib</code></p>
  <p>But in both cases <code>import MySQLdb</code> still tried to import <code>libmysqlclient.18.dylib</code>.</p>
  <p>Then I tried:<br />
  <code>$ pip install -U MySQL-python</code>
  and got:
  <code>Requirement already up-to-date: MySQL-python in /usr/local/lib/python2.7/site-packages</code></p>
  <h2>Existing answers</h2>
  <p>Many <a href="https://stackoverflow.com/questions/4546698/library-not-loaded-libmysqlclient-16-dylib-error-when-trying-to-run-rails-serv">answers</a> <a href="https://stackoverflow.com/questions/7375199/python-brew-and-mysqldb">to this</a> <a href="https://stackoverflow.com/questions/4559699/python-mysqldb-and-library-not-loaded-libmysqlclient-16-dylib">problem</a> <a href="https://stackoverflow.com/questions/6383310/python-mysqldb-library-not-loaded-libmysqlclient-18-dylib">on SO</a> suggest manually making an explicit symlink to the library with a version number (in my case <code>libmysqlclient.20.dylib</code>). However, this seems crude and not future-proof, given the existing symlinks:</p>
  <p>in <code>/usr/local/lib</code> there is<br />
  <code>libmysqlclient.dylib -&gt; ../Cellar/mysql/5.7.9/lib/libmysqlclient.dylib</code></p>
  <p>and in <code>/usr/local/Cellar/mysql/5.7.9/lib</code> we find:<br />
  <code>libmysqlclient.20.dylib</code></p>
  <p>with a symlink in the same directory to it:
  <code>libmysqlclient.dylib -&gt; libmysqlclient.20.dylib</code></p>
  <h2>How to make Python forget <code>libmysqlclient.18.dylib</code>?</h2>
  <p>So how can I get Python to forget <code>/usr/local/lib/libmysqlclient.18.dylib</code> and follow the correct symlink in in <code>/usr/local/lib</code> to <code>libmysqlclient.dylib</code>, without manually adding yet another symlink?</p>