4

Iam trying to develop a simple calculator program embeddeding python in .net,I wanna reference pythonnet from NuGet to include it in my project

I installed pythonnet v2.3.0 using NuGet,I also have python 3 installed in my system

It would be nice if some one give me step by step instruction to embedd python net

form1.cs code :

using System;
...
using Python.Runtime;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            PythonEngine.Initialize();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            int num1 = int.Parse(a.Text);
            int num2 = int.Parse(b.Text);
            result.Text = (num1 + num2).ToString();
            using (Py.GIL())
            {
                dynamic np = Py.Import("numpy");
            }
        }
     }
}

When I use using(Py.GIL()) line in my code it compiler shows

System.BadImageFormatException: 'Could not load file or assembly 'Python.Runtime, Version=2.3.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.'

3 Answers 3

5

This issue is likely caused by architecture (32-bit/64-bit) mismatch between Python.Runtime.dll and your Python.

pythonnet 2.3.0 NuGet packages currently published on nuget.org include two versions of the Python.Runtime.dll assembly: 32-bit(x86) and 64-bit(x64). There is a known issue with this package not installing the correct reference in the project, even if the project platform is set to 64-bit: https://github.com/pythonnet/pythonnet/issues/472.

Usually the x86 reference gets installed, and if your Python is 64-bit, you get the above exception.

To fix this:

  • Remove the existing assembly reference from the project.
  • Manually add reference to the correct assembly from the installed nuget package (e.g. your_solution_dir\packages\pythonnet_py35_dotnet.2.3.0\lib\net40\x64\Python.Runtime.dll)
Sign up to request clarification or add additional context in comments.

5 Comments

I removed the default python.runtime reference and refered "dir\packages\pythonnet_py35_dotnet.2.3.0\lib\net40\x64\Python.Runtime.dll" now I get a new error------->"System.DllNotFoundException: 'Unable to load DLL 'python35': The specified module could not be found. (Exception from HRESULT: 0x8007007E)'"
Are you using Python 3.5? Python.Runtime.dll is specific for Python version. Unfortunately, packages published on nuget.org are only for Python 3.5 (and 2.7).
Downloaded 3.5 and its working fine thankyou m8,Can i publish a application which uses python .NET without the user having python installed?
Please mark the answer as accepted if it works for you. You can also work with Python other as 3.5 by using other version of Python.Runtime.dll (refer to pythonnet wiki for options: github.com/pythonnet/pythonnet/wiki/Installation).
You can publish an application without the user having Python installed by packing a Python distribution together with your application. There are different options to achieve this, try searching for discussions on "embedding python".
2

Changed the CPU architecture from AnyCPU to x64 and it fixed the issue.

Comments

0

I had same problem. I was running KerasExampleWinApp Sample (keras.net). The error was:

"...Python.Runtime, Version=2.3.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies..."

To solve it, I just deleted the Nugget Reference and reinstalled it.

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.