Python beginner - No module named 'mpi4py'
Asked Answered
C

4

6

I'm a student and I'm using python for the first time.

Currently I'm using Xubuntu 19.10 on a virtual machine, I checked the python and mpi4py installation.

My professor of "Parallel programming" gave me this simple python code.

from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
print ("hello world from process ", rank)

I'm trying to run it but it doesn't works and I don't know how to fix it. When I try to execute, I receive this statement for each of the 4 process:

$ mpirun -n 4 python3 hello.py
Traceback (most recent call last):
     File "hello.py", line 3, in <module>
     from mpi4py import MPI
ModuleNotFoundError: No module named 'mpi4py'
Caravansary answered 25/11, 2019 at 13:25 Comment(2)
Install mpi4py?Heighho
you need to install mpi4py (hence the error in from mpi4py import MPI). Try pip install mpi4pyHorsemanship
C
6

you can install mpi4py via pip in your terminal

pip install mpi4py

or

 pip3 install mpi4py
Cepheus answered 25/11, 2019 at 13:27 Comment(4)
If I try it, then I receive Requirement already satisfied: mpi4py in /usr/lib/python2.7/dist-packages (3.0.2)Caravansary
You must then try pip3 install mpi4pyCepheus
In your case the pip is trying to install to python2.7 but you are trying to run you code with python3Cepheus
The issue is because python2 has the package but python3 doesntCepheus
L
6

If you are using 3.x version of python, could you try installing using

pip3 install mpi4py

Maybe you tried installing using just pip, and it might have installed to the 2.7 version of your python.

pip is for versions less than 3. pip3 is for versions 3.x

Langouste answered 25/11, 2019 at 13:30 Comment(0)
B
1

for Python3+: pip3 install mpi4py and for python lesser version: pip install mpi4py

Problem is the package is not installed

Bourque answered 25/11, 2019 at 13:29 Comment(0)
D
0

mpi4py is a package that depends on your mpi implementation. You need to install a MPI wrapper (i.e. OpenMpi). Then, you can echo $mpicc and make sure the path is correct. With these, you should be able to install mpi4py correctly with pip, pip install mpi4py You can use pip or pip3 as long as this commands point to the wanted distribution of python. pip --version

Divan answered 20/10, 2023 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.