I have a Python module that I would like to be able to run from the console, hence why I'm using pyinstaller. My module uses MPI and runs fine normally. Then I run the following:
pyinstaller my_script.py --hidden-import _mpi_dll_path
When I try to run the executable, I just get ImportError: cannot import name 'MPI' from 'mpi4py'. After a (slightly shameful) consultation of an LLM, I've got the impression that the problem is that import mpi4py builds the MPI class dynamically, and thus from mpi4py import MPI doesn't work in frozen executables.
I've also tried adding --collect-all mpi4py, but that doesn't help.
What is the fix here? If I can't use MPI directly, is there a function to get it after it has been built? Is there even a way to build it in an .exe?
I'm on Windows 11, with Python 3.13.7, mpi4py 4.1.1, and pyinstaller 6.16.0.
Full error:
Traceback (most recent call last):
File "cli.py", line 1, in <module>
from src.solver.__main__ import main, floor_plan_getters
File "pyimod02_importers.py", line 457, in exec_module
File "src\solver\__init__.py", line 1, in <module>
from .boundarycondition import BoundaryCondition, Dirichlet, Neumann, ConstantDirichlet, ConstantNeumann
File "pyimod02_importers.py", line 457, in exec_module
File "src\solver\boundarycondition.py", line 4, in <module>
from .types import Temperatures1D, MPI
File "pyimod02_importers.py", line 457, in exec_module
File "src\solver\types.py", line 5, in <module>
from mpi4py import MPI
ImportError: cannot import name 'MPI' from 'mpi4py' (C:\[redacted]\dist\solvefloorplan\_internal\mpi4py\__init__.py)
mpi4pypackage. This package is built on top of a MPI library so make sure you install one that is compatible with the one used to build mpi4py.MS-MPI)?