Skip to content

Python.Runtime.BadPythonDllException #54

Closed

Description

@woaidianqian

using System;
using System.IO;
using System.Threading.Tasks;
using Python.Runtime;

namespace Python.Deployment.Download
{
class Program
{
static async Task Main(string[] args)
{
// ================================================
// This example demonstrates how to download a Python distribution (v2.7.9) and install it locally
// ================================================

        // set the download source
        Python.Deployment.Installer.Source = new Deployment.Installer.DownloadInstallationSource()
        {
            DownloadUrl = @"https://www.python.org/ftp/python/3.7.3/python-3.7.3-embed-amd64.zip",
        };

        // install in local directory. if you don't set it will install in local app data of your user account
        Python.Deployment.Installer.InstallPath = Path.GetFullPath(".");

        // see what the installer is doing
        Python.Deployment.Installer.LogMessage += Console.WriteLine;

        // install from the given source
        await Python.Deployment.Installer.SetupPython();

        // ok, now use pythonnet from that installation
        PythonEngine.Initialize();

        // call Python's sys.version to prove we are executing the right version
        dynamic sys=Py.Import("sys");
        Console.WriteLine("### Python version:\n\t" + sys.version);

        // call os.getcwd() to prove we are executing the locally installed embedded python distribution
        dynamic os = Py.Import("os");
        Console.WriteLine("### Current working directory:\n\t" + os.getcwd());
        Console.WriteLine("### PythonPath:\n\t" + PythonEngine.PythonPath);

    }
}

}

PythonEngine.Initialize();
error!!!

Python.Runtime.BadPythonDllException:“Runtime.PythonDLL was not set or does not point to a supported Python runtime DLL. See https://github.com/pythonnet/pythonnet#embedding-python-in-net”

Activity

  1. henon commented on Jun 20, 2023

    @henon
    Owner

    did you compile this for x86 ?

  2. henon commented on Jun 20, 2023

    @henon
    Owner

    You may have to set this
    image

  3. woaidianqian commented on Jun 21, 2023

    @woaidianqian
    Author

    image
    I have already set it to x64, but the problem still persists. If I don't specify the Python version, it will automatically download Python 3.11 without any error. However, when I specify the Python version, it throws an error.

  4. woaidianqian commented on Jun 21, 2023

    @woaidianqian
    Author
    using System;
    using System.Threading.Tasks;
    using Python.Included;
    using Python.Runtime;
    
    namespace NetCoreExample
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                await Installer.SetupPython();
                PythonEngine.Initialize();
                dynamic sys=PythonEngine.ImportModule("sys");
                Console.WriteLine("Python version: " + sys.version);
            }
        }
    }
    dynamic sys=PythonEngine.ImportModule("sys");

    The last line should be changed to:

    dynamic sys=Py.Import("sys")
  5. henon commented on Jun 21, 2023

    @henon
    Owner

    and that solves it?

  6. woaidianqian commented on Jun 21, 2023

    @woaidianqian
    Author

    There is no error when the program automatically downloads python, and if you want to set the python version by yourself, it is still not solved

  7. henon commented on Jun 21, 2023

    @henon
    Owner

    I guess the newer version of pythonnet doesn't work with old python versions any more?

  8. rc14193 commented on Jun 20, 2025

    @rc14193

    Was a solution ever found for this? I am running into the same issue, but I don't want it to automatically download. I want to be able to specify the embedded resource. I changed the example project to .net9 and it works fine. I am not sure what additional configuration needs to be done. I'm also running as x64

    Image
  9. rc14193 commented on Jun 20, 2025

    @rc14193

    This appears to be some sort of namespace issue. The examples all say Python.Deployment. If I change to Python.Included though it will run. I am encountering a different error though. When I release to a different machine, the python it points to is the executable application and python is failing to import numpy. I'm assuming this is related in some way but I am not sure.
    My updated code.
    Image
    How it is in Python.Included examples
    Image

  10. henon commented on Jun 21, 2025

    @henon
    Owner

    Pasting screenshots of code is bad style. How should others copy your code? Use github's code block with automatic syntax hilighting instead.

  11. rc14193 commented on Jun 21, 2025

    @rc14193

    Sorry I meant it more to show I'm getting the same issue as OP here is the code

    private static async Task PreparePython()
    {
        // set the installation source to be the embedded python zip from our resources
        Python.Deployment.Installer.Source = new Python.Deployment.Installer.EmbeddedResourceInstallationSource()
        {
            Assembly = typeof(Program).Assembly,
            ResourceName = "python-3.11.0-embed-amd64.zip",
        };
    
        // install in local directory. if you don't set it will install in local app data of your user account
        Python.Deployment.Installer.InstallPath = Path.GetFullPath(".");
    
        // see what the installer is doing
        Python.Deployment.Installer.LogMessage += Console.WriteLine;
    
        // install from the given source
        await Python.Included.Installer.SetupPython();
        await Python.Included.Installer.InstallWheel(typeof(Program).Assembly,
            "numpy-2.3.0-cp311-cp311-win_amd64.whl");
    
        PythonEngine.Initialize();
        using (Py.GIL())
        {
            using (var scope = Py.CreateScope())
            {
                string code = @"import numpy as np;print('Hello World from python.')"; // Get the python file as raw text
                var scriptCompiled = PythonEngine.Compile(code); // Compile the code/file
                scope.Execute(scriptCompiled); // Execute the compiled python so we can start calling it.
            }
        }
    }
    

    I do have my project set to run as x64. I'm using the Python.NET version matching the projects in the examples and have been trying to investigate the cause locally and have pulled the latest Python.Included and using that as a project reference

    Image
  12. henon commented on Jun 21, 2025

    @henon
    Owner

    See https://github.com/pythonnet/pythonnet#embedding-python-in-net
    You might be just missing the line

    Runtime.Runtime.PythonDLL = "python311.dll"; // set the python dll to use, this is the one we just installed

    before calling

    PythonEngine.Initialize();
  13. rc14193 commented on Jun 23, 2025

    @rc14193

    Thank you that fixed it. I see now that the install wheel and embedded resource projects have that line in them, but the core and framework examples and README do not include that line. If I change what method I'm using to bundle python what determines if that line needs to be added?

  14. henon commented on Jun 23, 2025

    @henon
    Owner

    I see now that the install wheel and embedded resource projects have that line in them

    Yes, I just fixed that two days ago.

    Thank you that fixed it. I see now that the install wheel and embedded resource projects have that line in them, but the core and framework examples and README do not include that line. If I change what method I'm using to bundle python what determines if that line needs to be added?

    Honestly I don't know. I executed the examples and if they still worked I didn't change them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions