python / cpython

The Python programming language
https://www.python.org
Other
62.53k stars 30.01k forks source link

Embedded Python Crashes When Importing Torch With C API #106051

Closed tempaccountforissue closed 8 months ago

tempaccountforissue commented 1 year ago

Bug report

I'm trying to embed Python into a C++ based application using the Python/C API. If I don't include an embedded Python installation inside my projects folder, then the program uses my local Python install by default. Using the local install doesn't result in any errors and torch is able to import successfully. However, if I include a Windows embeddable package (64-bit) version of Python in my projects folder, then Python crashes after calling PyImport_ImportModule("importsFile") in my C++ file.

Inside of importsFile.pyI have the following code:

import numpy as np
import matplotlib
import torch

If I comment out import torch, then the other modules are able to import without error. However, if I leave import torch uncommented, then Python crashes.

I'm not sure if this is a bug on Python's side or PyTorch's side. Python only seems to crash when importing torch specifically, but it also only crashes when calling PyImport_ImportModule. So, it seems to me like it could be on either end.

Is this a known issue? How can I fix it?

Your environment

JelleZijlstra commented 1 year ago

Have you tried using a debugger to find the cause of the crash?

It's more likely that this is a bug in Torch than in CPython.

tempaccountforissue commented 1 year ago

@JelleZijlstra I've never used a debugger with the Python/C API. Are there any resources you know of that I could use for this?

JelleZijlstra commented 1 year ago

Any C++ debugger should do.

ethanhs commented 1 year ago

Also details about how you add torch to your import path/how you install it would help in replicating the crash.

tempaccountforissue commented 1 year ago

@ethanhs It's an embedded version of Python, so I followed this Stack Overflow post to get pip installed. Then I followed the instructions on PyTorch's website to install Torch.

I used the following configuration for installing PyTorch:

Do you need any other information?

iritkatriel commented 9 months ago

Have you contacted PyTorch about this? Core devs don't typically debug issues like this, which involve third party code, unless it's pretty clear that it's indeed a Python bug and there is no way to narrow it down - those cases are rare, and this doesn't seem to be one of them.

tempaccountforissue commented 9 months ago

@iritkatriel I created an issue on PyTorch's GitHub page around the same time I created this one. They have been (slowly) looking into it. I will update this issue if they find anything definitive.

iritkatriel commented 8 months ago

Thanks. If they find reason to believe there is a bug in python then please create an issue with a clear bug report.

gsohler commented 6 months ago

i got a similar issue. My embedded python with windows embeddable package crashes when importing numpy. Do you have any information for me ? how to use a c++ debugger in windows ?