Pruebe la declaración en Cython para cimport (para usar con mpi4py)
¿Hay alguna manera de tener el equivalente de la declaración de prueba de Python en Cython para la importación?
Algo como eso:
try:
cimport something
except ImportError:
pass
Necesitaría esto para escribir una extensión de Cython que se pueda compilar con o sin mpi4py. Esto es muy estándar en lenguajes compilados donde los comandos mpi se pueden colocar entre las directivas de preprocesador #ifdef y #endif. ¿Cómo podemos obtener el mismo resultado en Cython?
Intenté esto pero no funciona:
try:
from mpi4py import MPI
from mpi4py cimport MPI
from mpi4py.mpi_c cimport *
except ImportError:
rank = 0
nb_proc = 1
# solve a incompatibility between openmpi and mpi4py versions
cdef extern from 'mpi-compat.h': pass
does_it_work = 'Not yet'
En realidad funciona bien si mpi4py está instalado correctamente pero siimport mpi4py
plantea un ImportError, el archivo Cython no se compila y aparece el error:
Error compiling Cython file:
------------------------------------------------------------
...
try:
from mpi4py import MPI
from mpi4py cimport MPI
^
------------------------------------------------------------
mod.pyx:4:4: 'mpi4py.pxd' not found
El archivosetup.py
:
from setuptools import setup, Extension
from Cython.Distutils import build_ext
import os
here = os.path.abspath(os.path.dirname(__file__))
include_dirs = [here]
try:
import mpi4py
except ImportError:
pass
else:
INCLUDE_MPI = '/usr/lib/openmpi/include'
include_dirs.extend([
INCLUDE_MPI,
mpi4py.get_include()])
name = 'mod'
ext = Extension(
name,
include_dirs=include_dirs,
sources=['mod.pyx'])
setup(name=name,
cmdclass={"build_ext": build_ext},
ext_modules=[ext])