Calling python from VB.NET: can't load numpy/matplotlib

Aug 14 at 1:39 PM
Hallo,

I am trying to call python functions/modules from VB.NET. All things went well if I am using only native Python modules such as "os" or "itertools". But when I am trying to load "numpy" or "matplotlib" an exception is raised "cannot import multiarray from numpy.core".

Does any one how to solve this problem? Are the packages numpy and matplotlib supported by PTVS?
Imports Microsoft.Scripting
Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting

Module Module1

    Sub Main()
        Dim py = Python.CreateRuntime()
        Dim engine = Python.CreateEngine()
        Dim sp = engine.GetSearchPaths()
               sp.Add("C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64")
        sp.Add("C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib")
        sp.Add("C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\DLLs")
        sp.Add("C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib\site-packages")
        engine.SetSearchPaths(sp)
        Dim myModule = engine.ImportModule("numpy")
    End Sub
End Module
Coordinator
Aug 14 at 4:43 PM
PTVS is just the IDE, it doesn't handle loading and running code at runtime. This is rather about Python itself (i.e. you'll get the same problem if you're running this code directly from command line).

Judging by the code snippet, you're using IronPython, but then trying to make it load packages from your Canopy install. Canopy is not IronPython-based, it's a CPython distro. And CPython native binary modules are not supported by IronPython - and numpy in particular has a lot of those.

If you want to use Python from VB.NET, and you need numpy, your best option at the moment is to ignore IronPython and host the normal Python interpreter (Canopy will work as well). You can either P/Invoke directly into the Python extensibility/hosting C APIs, or use some existing wrapper, such as pythondotnet.
Aug 15 at 6:44 AM
Thanks for your quick answer. I run into a lot of problems during trying to build an IronPython with numpy for my own. I will try P/invoke to call the Pythons scripts which I intent to use. Nevertheless, I have to provide an windows installer which ships with all Progs and files needed to run the application, also Python. I thought that PTVS is a good way to do so, but it does not seems to be the case.
Coordinator
Aug 15 at 7:19 AM
numpy on IronPython is basically just not a supported scenario (from either side). Enthought was providing a port of numpy to IronPython a while ago, but it had languished and is not supported for a long time now.

PTVS is a Python IDE - it helps you write and debug Python code, but it is not a Python implementation (nor does it ship with one), so it will, indeed, not help you solve this particular problem. I would still recommend trying pythondotnet first, though, before going for P/Invoke. It has much nicer APIs (both on .NET side and on Python side) that resemble IronPython ones pretty closely, but let you work with a native Python implementation that supports all the popular libraries directly.
Aug 15 at 12:51 PM
pythondotnet is good solution, but it uses some unusual hacking of the inter-operability between CLR (managed) and Python (native). I found that you need a full fledged package to load it into embedded Python, I could not load my own modules unless setup as a package. The alternative is to try C++/CLI, but you will have to manage all conversions. C++/CLI is also good for supporting C API for Numpy and Boost or even Boost.Numpy if you have to deal with nd-arrays.