On the official website of Python.NET, it says it supports Python 3.8. Great. Now I am interested to call my existing python 3.8 modules from an application developed in C# (.NET framework v4.7.2).
EDITS:
Since close to no installation instructions are given by the authors of Python.NET for calls from C#, I followed the instructions given here. Note that I am using miniconda3 and python 32bit (the latter being required for my project) so I did the following:
From VScode, I installed pythonnet with
pip install
as well as all the required python packages (let's usenumpy
here for the purpose of the example) under the "C:\ProgramData\Miniconda3\envs\py38_32" environment.Set environment paths in C# (in VS2019) :
string pythonPath1 = @"C:\ProgramData\Miniconda3\envs\py38_32";
string pythonPath2 = @"C:\ProgramData\Miniconda3\envs\py38_32\Lib\site-packages";
Environment.SetEnvironmentVariable("PATH", pythonPath1, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PYTHONHOME", pythonPath1, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PYTHONPATH", pythonPath2, EnvironmentVariableTarget.Process);
Referenced the Python.Runtime.dll from the "py38_32\Lib\site-packages" folder in my project and added
using Python.Runtime;
Tried to import numpy:
using (Py.GIL()) { dynamic np = Py.Import("numpy"); //fail on this line with ImportError }
There it failed on the numpy import, with "Python.Runtime.PythonException: 'ImportError". It also says "The Python version is: Python3.8 from "\bin\Debug\MyProject.exe"... seems like it looks for python in my debug folder, and not in the path mentionned above... maybe... And importing numpy from VScode using the same environment works totally fine.
Anyone with a clue of what's going on here ?