[Python] Can't import my module with Python.NET from the same directory as the project

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a .NET project and encountered this issue when i decided to try embedding some Python code a different person wrote. I couldn't import his module with Py.Import and got Python.Runtime.PythonException: No module named 'mypythonscript' The module is in the same directory as the code. I even checked the working directory with os.getcwd() to make sure I didn't mess up with directories. I ended up creating a seperate project to experiment and couldn't import the module in it too. Here's the code of the test project

    using Python.Runtime;


    Runtime.PythonDLL = @"*my python311.dll path*";
    PythonEngine.Initialize();
    using (Py.GIL())
    {
    var recognizerModule = Py.Import("mypythonscript");

    }


    mypythonscript.py is in the project directory and looks like this (although I don't think it matters what's inside it)

    def say_hello():
    return "Hello!"


    I've read the documentation, some stack overflow threads and watched a couple of videos. Everyone seems to do exactly what I did and everything works for them. Someone please help me I'm losing my mind.

    p.s. Importing a built-in module like os works fine

    Continue reading...

Compartilhe esta Página