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 ?

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, the pip install was already mentioned in the question. But thanks for advertising unsupported alternatives...
The unsupported version has since been merged into the official Python.NET: nuget.org/packages/pythonnet
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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.