Unable to import a custom DLL in python
<p>I am trying to expose a C++ class to python with <code>boost::python</code>, so I am going through <a href="http://www.boost.org/doc/libs/1_61_0/libs/python/doc/html/tutorial/tutorial/exposing.html" rel="nofollow">this tutorial</a>. I created a visual studio <code>.dll</code> project, with this source code:</p>

<pre><code>#include &lt;boost/python.hpp&gt;
using namespace boost::python;

struct World
{
    void set(std::string msg) { this-&gt;msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};

BOOST_PYTHON_MODULE(hello)
{
    class_&lt;World&gt;("World")
        .def("greet", &amp;World::greet)
        .def("set", &amp;World::set)
    ;
}
</code></pre>

<p>And I built it as a 64-bit dll. The next step in the tutorial says:</p>

<blockquote>
  <p>Here, we wrote a C++ class wrapper that exposes the member functions greet and set. Now, after building our module as a shared library, we may use our class World in Python. Here's a sample Python session:</p>
</blockquote>

<pre><code>&gt;&gt;&gt; import hello
&gt;&gt;&gt; planet = hello.World()
&gt;&gt;&gt; planet.set('howdy')
&gt;&gt;&gt; planet.greet()
'howdy'
</code></pre>

<p>However, after launching python in the same directory and typing <code>import hello</code> I get</p>

<pre><code>&gt;&gt;&gt; import hello
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ImportError: No module named 'hello'
&gt;&gt;&gt;
</code></pre>

<p>I have tried renaming the 'dll' files to <code>hello.dll</code>, and also copying <em>every</em> output file (<code>dll</code>, <code>exp</code>, <code>ilk</code>, <code>lib</code>, and <code>pdb</code>) to <code>%PYTHONPATH%\DLLs</code>, yet I am still unable to import the module into python.</p>

<p>Much googling brought me to <a href="http://wolfprojects.altervista.org/articles/dll-in-c-for-python/" rel="nofollow">this article</a> recommending I use <code>ctypes</code> to import the <code>dll</code>. This lets me load the <code>dll</code>, but I am still unable to call the "World" class. For example:</p>

<pre><code>&gt;&gt;&gt; import ctypes
&gt;&gt;&gt; mydll = ctypes.cdll.LoadLibrary("hello")
&gt;&gt;&gt; mydll
&lt;CDLL 'hello', handle 7fef40a0000 at 0x775ba8&gt;
&gt;&gt;&gt; hello = mydll.World()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "C:\Program Files\Python35\lib\ctypes\__init__.py", line 360, in __getatt
r__
    func = self.__getitem__(name)
  File "C:\Program Files\Python35\lib\ctypes\__init__.py", line 365, in __getite
m__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'World' not found
&gt;&gt;&gt;
</code></pre>

<p>So a couple questions:</p>

<ol>
<li><p>Is it possible to import a <code>dll</code> in python <em>without</em> using ctypes? The tutorial seems to indicate that it is, but doesn't provide very much detail about the right way to get the dll into python.</p></li>
<li><p>Which files do I need and where? It seems like I should only need the <code>dll</code> file from visual studio in the working directory of my python shell, but this is clearly not working for me.</p></li>
<li><p>Why can't I call <code>World</code> through ctypes?</p></li>
</ol>

<p>Some more important details: I am using windows 7 64-bit, python 3.5.2 64-bit, and Visual Studio 2015 with Boost 1.61.</p>

<p>I actually just figured out the answer shortly after posting the question. Thanks to <a href="http://mmmovania.blogspot.com/2013/01/running-c-code-from-python-using.html" rel="nofollow">this blog post</a> I found out that simply renaming the <code>hello.dll</code> to <code>hello.pyd</code> was enough. From some more googling, I <em>think</em> that <code>ctypes</code> is only for C DLLs, not C++, and <em>not</em> with boost!. The point of <code>boost::python</code>, is to remove the need for ctypes and make the DLL compatible with python. So to answer all of my own questions:</p>

<ol>
<li><p>Yes, but it must have a <code>.pyd</code> extension.</p></li>
<li><p>You only need the compiled <code>dll</code> file and the <code>boost_python_vc140...dll</code> (may vary). However, like I said, the <code>dll</code> file must be renamed.</p></li>
<li><p>Because ctypes is not the right tool for loading a <code>boost::python</code> dll!</p></li>
</ol>

python dll ctypes boost-python python-c-api