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 · 1 comment
Open

Appending library directory to PythonPath causes crash #2325

pm64 opened this issue Feb 19, 2024 · 1 comment

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

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

2 participants