0

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)
6
  • always put full error message (traceback) because there are other useful information. Commented Oct 10 at 22:39
  • maybe it needs to add some C/C++ libraries. Commented Oct 10 at 22:40
  • First you need to install the python mpi4py package. 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. Commented Oct 11 at 5:06
  • As I said, my program runs fine normally, multithreaded or not. It's not that I haven't installed mpi4py. Commented Oct 11 at 14:28
  • Did you install a MPI a library (e.g. MS-MPI)? Commented Oct 11 at 15:20

1 Answer 1

0

This is not a complete solution, but for my purposes (being able to run the module from the command line with arguments) I found a workaround. I found out that calling python script.py [options...] forwards those given options to sys.argv in script.py, which I could then read normally using argparser. That was my real objective, not necessarily making a distributable executable, so this worked out fine.

I'm still open for a solution to my original question though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.