title: python loading c lib with CDLL, doesn't see libraries in the python path
body: <p>I'm trying to get some open source academic code working (the project home is <a href="http://lammps.sandia.gov/" rel="noreferrer">here</a>). It is a big C++ codebase with a (very) thin python wrapper which uses <code>CDLL</code> to load the C++ and call some C functions that are available to allow primitive python scripting of the code.</p>
  
  <p>However, the initial import code crashes because it can't find the .so files sitting next to it in site-packages:</p>
  
  <p>in the installed file:</p>
  
  <pre class="lang-py prettyprint-override"><code>from ctypes import *
  
  try:
    self.lib = CDLL("_lammps.so")
  except:
    try:
      self.lib = CDLL("_lammps_serial.so")
    except:
      raise OSError,"Could not load LAMMPS dynamic library"
  </code></pre>
  
  <p>and in a script or the interpreter:</p>
  
  <pre class="lang-py prettyprint-override"><code>from lammps import lammps
  l = lammps()
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "lammps.py", line 42, in __init__
      raise OSError,"Could not load LAMMPS dynamic library"
  OSError: Could not load LAMMPS dynamic library
  </code></pre>
  
  <p>Other answers <a href="https://stackoverflow.com/questions/2980479/python-ctypes-loading-dll-from-from-a-relative-path">might seem to have this covered</a>, but this only works if <code>CDLL()</code> is called within the script actually invoked (or the working directory of the prompt that ran the interpreter) - i.e. if the 'relative path' is in user-space, rather than python-library-space.</p>
  
  <p>How do we reliably install for import a C/C++ library that we built ourselves? Short of polluting the system library locations like <code>/usr/lib</code>, which isn't very pythonic, I can't see an easy solution.</p>
  
  <p>(EDIT: corrected function names, unclear refactoring unhelpful! sorry!)</p>