lundi 27 juin 2016

Cython PYD is a DLL, is there an easy way to embed this as a C++ DLL in other apps?

I have quite a bit of Cython code and those familiar it generates a C++ .cpp file from the .pyx file and compiles into a DLL (PYD) with Python info added for importing into Python. https://docs.python.org/3/faq/windows.html#is-a-pyd-file-the-same-as-a-dll I'm trying to import this into MATLAB which requires a .h file (which is not generated by Cython) using loadlibrary: http://www.mathworks.com/help/matlab/ref/loadlibrary.html

To ensure this works on Windows machines lacking my version of Python, I am using the embedded library for 3.5.1 here: https://docs.python.org/3.5/using/windows.html#embedded-distribution

And before calling Py_Initialize(), I extract the python35.zip file from the embedded distribution and place it in the project directory, then reference it as described here: http://www.myoddweb.com/2016/02/27/embed-python-in-your-c-application-without-python-installed-on-guest-machine/ i.e.

#include <Python.h>
std::wstring python_path;
python_path += L"\python35.zip";
Py_SetPath(python_path.c_str());
Py_Initialize();

In Visual Studio 2015 I've tried using the python35.lib through the linker and without it just copying in the python35.dll and other dlls included to the directory containing the compiled .exe but to no avail, using this sample project https://github.com/Who8MyLunch/Python-Embed-Demo

Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'

Really just stumped here. I have tried to keep Python separate from my system build. I'm still referencing the Numpy includes from my system path though C:Anaconda3Libsite-packagesnumpycoreinclude not sure if that is the issue or what. All my code is fast because I'm using NumPy MemoryViews (is this pointless since I'm going to be calling it from other apps? I could easily substitute from libcpp.vector cimport vector but others have reported this is extremely slow compared to NumPy). Not a MSVC expert here so just looking for anyone who can guide me in the right direction. Much appreciated.

1 commentaire: