JuliaPy / PythonCall.jl

Python and Julia in harmony.
https://juliapy.github.io/PythonCall.jl/stable/
MIT License
720 stars 61 forks source link

Add warning and hint when loading torch before juliacall #385

Closed LilithHafner closed 8 months ago

LilithHafner commented 8 months ago

Before

x@x pythoncall % python -c 'import torch; from torch.autograd import Function; import juliacall'
zsh: segmentation fault  python3 -c 
x@x pythoncall % be sad
zsh: command not found: be

After

x@x pythoncall % python -c 'import torch; from torch.autograd import Function; import juliacall'
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/juliacall/__init__.py:62: UserWarning: `torch` was loaded before the juliacall. This may cause a segfault. To avoid this, import `juliacall` *before* importing `torch`. For updates, see https://github.com/pytorch/pytorch/issues/78829
  warnings.warn(
zsh: segmentation fault  python3 -c 
x@x pythoncall % python -c 'import juliacall; import torch; from torch.autograd import Function'
x@x

Helps with https://github.com/JuliaPy/PythonCall.jl/issues/384, but doesn't solve the issue satisfactorily.

LilithHafner commented 8 months ago

"Swap the import order and your segfaults will go away"