1

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:

  1. From VScode, I installed pythonnet with pip install as well as all the required python packages (let's use numpy here for the purpose of the example) under the "C:\ProgramData\Miniconda3\envs\py38_32" environment.

  2. 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);
  1. Referenced the Python.Runtime.dll from the "py38_32\Lib\site-packages" folder in my project and added using Python.Runtime;

  2. 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 ?

bricx
  • 593
  • 4
  • 18

2 Answers2

0

The official way to install Python.NET is via Python package manager pip.

On Windows, that would be python.exe -m pip install pythonnet. That will create Python.Runtime.dll somewhere in your Python distribution. Just reference it from your project, and ensure architectures match.

An unsupported alternative is to use my unofficial build, and set Runtime.PythonDLL to the location of python38.dll of your choice (you can find one at runtime using another NuGet package: WhichPython).

LOST
  • 2,956
  • 3
  • 25
  • 40
  • Yes, the pip install was already mentioned in the question. But thanks for advertising unsupported alternatives... – bricx May 11 '21 at 16:39
  • The unsupported version has since been merged into the official Python.NET: https://www.nuget.org/packages/pythonnet – LOST May 12 '21 at 17:58
0

As mentioned here, Anaconda (and Miniconda) do not works with Python.NET. All I had to do was to reinstall a fresh copy of Python with all the package I needed and replace the following piece of code with the new paths:

string pythonPath1 = @"C:\Python";
string pythonPath2 = @"C:\Python\Lib\site-packages";
bricx
  • 593
  • 4
  • 18