Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appending library directory to PythonPath causes crash #2325

Open
pm64 opened this issue Feb 19, 2024 · 4 comments
Open

Appending library directory to PythonPath causes crash #2325

pm64 opened this issue Feb 19, 2024 · 4 comments

Comments

@pm64
Copy link

pm64 commented Feb 19, 2024

Environment

  • Pythonnet version: 3.0.3
  • Python version: 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]
  • Operating System: Windows and Linux
  • .NET Runtime: 8.0

Details

The Python my app calls has a number of dependencies. For predictability, I want to bundle the dependencies with my app, rather than obtain them via pip at runtime. I gathered the entire dependency tree, as well as the .py file with the code I plan to directly call, and added it all to my Visual Studio project, using build action "Content" and CopyToOutputDirectory=PreserveNewest for each item.

If I didn't need these dependencies, I would call:

PythonEngine.Initialize();

That works great. But since I do require the dependencies, I append their root folder to PythonPath first:

var libPath = Path.Combine(Directory.GetCurrentDirectory(), "Python", "libs");
PythonEngine.PythonPath = PythonEngine.PythonPath + ";" + libPath;
PythonEngine.Initialize();

Simply appending libPath to PythonPath causes my app to crash on execution of PythonEngine.Initialize();

  • If there was a crash, please include the traceback here.

Is there a recommended way to obtain the traceback? The crash causes the app to exit immediately, there is no exception caught by the debugger. I am more than happy to gather any diagnostic info needed.

@iPromKnight
Copy link

iPromKnight commented Mar 28, 2024

By no means an expert, in fact been using this for a day lol
BUt this is what I did

private void InitModules()
    {
        using var py = Py.GIL();
        _sys = Py.Import(SysModuleName);

        if (_sys == null)
        {
            _logger.LogError($"Failed to import Python module: {SysModuleName}");
            return;
        }

        _sys.path.append(Path.Combine(AppContext.BaseDirectory, "python"));

        _rtn = Py.Import(RtnModuleName);
        if (_rtn == null)
        {
            _logger.LogError($"Failed to import Python module: {RtnModuleName}");
        }
    }
    ```
    
    Where the packages I have bundled are in a python folder that gets copied to output dir

@duxuan11
Copy link

环境

  • Pythonnet版本:3.0.3
  • Python 版本:3.11.0 (main,2022 年 10 月 24 日,18:26:48) [MSC v.1933 64 位 (AMD64)]
  • 操作系统:Windows和Linux
  • .NET 运行时:8.0

细节

我的应用程序调用的 Python 有许多依赖项。为了可预测性,我希望将依赖项与我的应用程序捆绑在一起,而不是在运行时通过 pip 获取它们。我收集了整个依赖项树以及包含我计划直接调用的代码的 .py 文件,并将其全部添加到我的 Visual Studio 项目中,对每个项目使用构建操作“Content”和 CopyToOutputDirectory=PreserveNewest。

如果我不需要这些依赖项,我会调用:

PythonEngine.Initialize();

效果很好。但由于我确实需要依赖项,因此我首先将它们的根文件夹附加到 PythonPath:

var libPath = Path.Combine(Directory.GetCurrentDirectory(), "Python", "libs");
PythonEngine.PythonPath = PythonEngine.PythonPath + ";" + libPath;
PythonEngine.Initialize();

只需将 libPath 附加到 PythonPath 就会导致我的应用程序在执行 PythonEngine.Initialize() 时崩溃;

  • 如果发生崩溃,请在此处包含回溯。

有没有推荐的方法来获取回溯?崩溃导致应用程序立即退出,调试器没有捕获任何异常。我非常乐意收集所需的任何诊断信息。

Do you have any solution?

@lostmsu
Copy link
Member

lostmsu commented Aug 20, 2024

On Windows you should be able to see the crash by debugging your app with mixed-mode debugging (or just native debugging) turned on. At least we need to know what kind of crash you experience.

@duxuan11
Copy link

Environment

  • Pythonnet version: 3.0.3
  • Python version: 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]
  • Operating System: Windows and Linux
  • .NET Runtime: 8.0

Details

The Python my app calls has a number of dependencies. For predictability, I want to bundle the dependencies with my app, rather than obtain them via pip at runtime. I gathered the entire dependency tree, as well as the .py file with the code I plan to directly call, and added it all to my Visual Studio project, using build action "Content" and CopyToOutputDirectory=PreserveNewest for each item.

If I didn't need these dependencies, I would call:

PythonEngine.Initialize();

That works great. But since I do require the dependencies, I append their root folder to PythonPath first:

var libPath = Path.Combine(Directory.GetCurrentDirectory(), "Python", "libs");
PythonEngine.PythonPath = PythonEngine.PythonPath + ";" + libPath;
PythonEngine.Initialize();

Simply appending libPath to PythonPath causes my app to crash on execution of PythonEngine.Initialize();

  • If there was a crash, please include the traceback here.

Is there a recommended way to obtain the traceback? The crash causes the app to exit immediately, there is no exception caught by the debugger. I am more than happy to gather any diagnostic info needed.

I solved this problem in a very clever way. You want to include a custom path in the path so that PythonNet can find the custom Python script. Instead, you can use (sys.path.append(path)) to add a custom path to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants