Paramétrer le compilateur fortran dans f2py

J’essaie d’exécuter l’ exemple f2py pour créer le module d’extension compilé¶:

# import os # os.environ["CC"] = "gcc" # os.environ["CXX"] = "g++" # Using post-0.2.2 scipy_distutils to display fortran comstackrs from scipy_distutils.fcomstackr import new_fcomstackr comstackr = new_fcomstackr() # or new_fcomstackr(comstackr='intel') comstackr.dump_properties() #Generate add.f wrapper from numpy import f2py with open("add.f") as sourcefile: sourcecode = sourcefile.read() print 'Fortran code' print sourcecode # f2py.comstack(sourcecode, modulename='add', extra_args = '--comstackr=gnu --fcomstackr=gnu') f2py.comstack(sourcecode, modulename='add', extra_args = '--fcomstackr=gfortran') # f2py.comstack(sourcecode, modulename='add') import add 

Cependant, le code ne fonctionne pas. Je crois que c’est parce qu’il n’utilise pas le compilateur installé (le système opérationnel est Ubuntu 12.04). Cependant, malgré l’utilisation de l’argument “–fcomstack” ou la modification des entrées “CC” et “CXX”, cela ne fonctionne toujours pas ….

Ceci est le journal complet des erreurs. S’il vous plaît n’importe quel conseil serait le bienvenu:

 Could not locate executable g77 Could not locate executable f77 customize GnuFComstackr Could not locate executable ifort Could not locate executable ifc Could not locate executable efort Could not locate executable efc Could not locate executable ifort customize IntelFComstackr customize LaheyFComstackr customize PGroupFComstackr customize AbsoftFComstackr customize NAGFComstackr customize VastFComstackr customize GnuFComstackr customize CompaqFComstackr customize IntelItaniumFComstackr customize G95FComstackr customize GnuFComstackr GnuFComstackr instance properties: archiver = ['ar', '-cr'] comstack_switch = '-c' comstackr_f77 = ['f77', '-Wall', '-fno-second-underscore'] comstackr_f90 = None comstackr_fix = None libraries = [] library_dirs = [] linker_so = ['f77', '-Wall'] object_switch = '-o ' ranlib = ['ranlib'] version_cmd = ['f77', '--version'] Fortran code C SUBROUTINE ZADD(A,B,C,N) C DOUBLE COMPLEX A(*) DOUBLE COMPLEX B(*) DOUBLE COMPLEX C(*) INTEGER N DO 20 J = 1, N C(J) = A(J)+B(J) 20 CONTINUE END Unknown vendor: "gfortran" running build running config_cc unifing config_cc, config, build_clib, build_ext, build commands --comstackr options running config_fc unifing config_fc, config, build_clib, build_ext, build commands --fcomstackr options running build_src build_src building extension "add" sources f2py options: [] f2py:> /tmp/tmpNKECn3/src.linux-x86_64-2.7/addmodule.c creating /tmp/tmpNKECn3/src.linux-x86_64-2.7 Reading fortran codes... Reading file '/tmp/tmpi2WwBV.f' (format:fix,ssortingct) Post-processing... Block: add Block: zadd Post-processing (stage 2)... Building modules... Building module "add"... Constructing wrapper function "zadd"... getarrdims:warning: assumed shape array, using 0 instead of '*' getarrdims:warning: assumed shape array, using 0 instead of '*' getarrdims:warning: assumed shape array, using 0 instead of '*' zadd(a,b,c,n) Wrote C/API module "add" to file "/tmp/tmpNKECn3/src.linux-x86_64-2.7/addmodule.c" adding '/tmp/tmpNKECn3/src.linux-x86_64-2.7/fortranobject.c' to sources. adding '/tmp/tmpNKECn3/src.linux-x86_64-2.7' to include_dirs. copying /usr/local/lib/python2.7/dist-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmpNKECn3/src.linux-x86_64-2.7 copying /usr/local/lib/python2.7/dist-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmpNKECn3/src.linux-x86_64-2.7 build_src: building npy-pkg config files running build_ext customize UnixCComstackr customize UnixCComstackr using build_ext customize Gnu95FComstackr Found executable /usr/bin/gfortran Found executable /home/vital/Iraf/unix/hlib/f77.sh Found executable /usr/bin/ranlib customize Gnu95FComstackr using build_ext building 'add' extension compiling C sources C comstackr: gfortran -fno-ssortingct-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wssortingct-prototypes -fPIC creating /tmp/tmpNKECn3/tmp creating /tmp/tmpNKECn3/tmp/tmpNKECn3 creating /tmp/tmpNKECn3/tmp/tmpNKECn3/src.linux-x86_64-2.7 comstack options: '-I/tmp/tmpNKECn3/src.linux-x86_64-2.7 -I/usr/local/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c' gfortran: /tmp/tmpNKECn3/src.linux-x86_64-2.7/addmodule.c In file included from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1781:0, from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarrayobject.h:18, from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/arrayobject.h:4, from /tmp/tmpNKECn3/src.linux-x86_64-2.7/fortranobject.h:13, from /tmp/tmpNKECn3/src.linux-x86_64-2.7/addmodule.c:19: /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp] #warning "Using deprecated NumPy API, disable it by " \ ^ /tmp/tmpNKECn3/src.linux-x86_64-2.7/addmodule.c:105:12: warning: 'f2py_size' defined but not used [-Wunused-function] static int f2py_size(PyArrayObject* var, ...) ^ gfortran: /tmp/tmpNKECn3/src.linux-x86_64-2.7/fortranobject.c In file included from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1781:0, from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarrayobject.h:18, from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/arrayobject.h:4, from /tmp/tmpNKECn3/src.linux-x86_64-2.7/fortranobject.h:13, from /tmp/tmpNKECn3/src.linux-x86_64-2.7/fortranobject.c:2: /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp] #warning "Using deprecated NumPy API, disable it by " \ ^ compiling Fortran sources Fortran f77 comstackr: /home/vital/Iraf/unix/hlib//f77.sh -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops Fortran f90 comstackr: /usr/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops Fortran fix comstackr: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops comstack options: '-I/tmp/tmpNKECn3/src.linux-x86_64-2.7 -I/usr/local/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c' f77.sh:f77: /tmp/tmpi2WwBV.f Cannot open file tmpi2WwBV.f gcc: error: tmpi2WwBV.c: No such file or directory gcc: fatal error: no input files compilation terminated. Cannot open file tmpi2WwBV.f gcc: error: tmpi2WwBV.c: No such file or directory gcc: fatal error: no input files compilation terminated. error: Command "/home/vital/Iraf/unix/hlib//f77.sh -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops -I/tmp/tmpNKECn3/src.linux-x86_64-2.7 -I/usr/local/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c -c /tmp/tmpi2WwBV.f -o /tmp/tmpNKECn3/tmp/tmpi2WwBV.o" failed with exit status 4 Traceback (most recent call last): File "/home/vital/git/pyResources/pyResources/Tutorials/test_f2py.py", line 21, in  import add ImportError: No module named add 

PS Iraf est un logiciel astronomique et non un compilateur.

Modifier 1:

En utilisant l’importation correcte suggérée par jthomas, j’obtiens une erreur plus courte, mais le système essaie toujours d’utiliser le script situé dans le dossier IRAF. Voici le message d’erreur complet utilisant ce code:

 Gnu95FComstackr instance properties: comstack_switch = '-c' libraries = [] library_dirs = [] object_switch = '-o ' Fortran code C SUBROUTINE ZADD(A,B,C,N) C DOUBLE COMPLEX A(*) DOUBLE COMPLEX B(*) DOUBLE COMPLEX C(*) INTEGER N DO 20 J = 1, N C(J) = A(J)+B(J) 20 CONTINUE END Unknown vendor: "gfortran" running build running config_cc unifing config_cc, config, build_clib, build_ext, build commands --comstackr options running config_fc unifing config_fc, config, build_clib, build_ext, build commands --fcomstackr options running build_src build_src building extension "add" sources f2py options: [] f2py:> /tmp/tmp_KRxI5/src.linux-x86_64-2.7/addmodule.c creating /tmp/tmp_KRxI5/src.linux-x86_64-2.7 Reading fortran codes... Reading file '/tmp/tmph5mvme.f' (format:fix,ssortingct) Post-processing... Block: add Block: zadd Post-processing (stage 2)... Building modules... Building module "add"... Constructing wrapper function "zadd"... getarrdims:warning: assumed shape array, using 0 instead of '*' getarrdims:warning: assumed shape array, using 0 instead of '*' getarrdims:warning: assumed shape array, using 0 instead of '*' zadd(a,b,c,n) Wrote C/API module "add" to file "/tmp/tmp_KRxI5/src.linux-x86_64-2.7/addmodule.c" adding '/tmp/tmp_KRxI5/src.linux-x86_64-2.7/fortranobject.c' to sources. adding '/tmp/tmp_KRxI5/src.linux-x86_64-2.7' to include_dirs. copying /usr/local/lib/python2.7/dist-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmp_KRxI5/src.linux-x86_64-2.7 copying /usr/local/lib/python2.7/dist-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmp_KRxI5/src.linux-x86_64-2.7 build_src: building npy-pkg config files running build_ext customize UnixCComstackr customize UnixCComstackr using build_ext customize Gnu95FComstackr Found executable /usr/bin/gfortran Found executable /home/vital/Iraf/unix/hlib/f77.sh Found executable /usr/bin/ranlib customize Gnu95FComstackr using build_ext building 'add' extension compiling C sources C comstackr: gcc -fno-ssortingct-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wssortingct-prototypes -fPIC creating /tmp/tmp_KRxI5/tmp creating /tmp/tmp_KRxI5/tmp/tmp_KRxI5 creating /tmp/tmp_KRxI5/tmp/tmp_KRxI5/src.linux-x86_64-2.7 comstack options: '-I/tmp/tmp_KRxI5/src.linux-x86_64-2.7 -I/usr/local/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c' gcc: /tmp/tmp_KRxI5/src.linux-x86_64-2.7/addmodule.c In file included from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1781:0, from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarrayobject.h:18, from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/arrayobject.h:4, from /tmp/tmp_KRxI5/src.linux-x86_64-2.7/fortranobject.h:13, from /tmp/tmp_KRxI5/src.linux-x86_64-2.7/addmodule.c:19: /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp] #warning "Using deprecated NumPy API, disable it by " \ ^ /tmp/tmp_KRxI5/src.linux-x86_64-2.7/addmodule.c:105:12: warning: 'f2py_size' defined but not used [-Wunused-function] static int f2py_size(PyArrayObject* var, ...) ^ gcc: /tmp/tmp_KRxI5/src.linux-x86_64-2.7/fortranobject.c In file included from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1781:0, from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarrayobject.h:18, from /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/arrayobject.h:4, from /tmp/tmp_KRxI5/src.linux-x86_64-2.7/fortranobject.h:13, from /tmp/tmp_KRxI5/src.linux-x86_64-2.7/fortranobject.c:2: /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp] #warning "Using deprecated NumPy API, disable it by " \ ^ compiling Fortran sources Fortran f77 comstackr: /home/vital/Iraf/unix/hlib//f77.sh -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops Fortran f90 comstackr: /usr/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops Fortran fix comstackr: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops comstack options: '-I/tmp/tmp_KRxI5/src.linux-x86_64-2.7 -I/usr/local/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c' f77.sh:f77: /tmp/tmph5mvme.f Cannot open file tmph5mvme.f gcc: error: tmph5mvme.c: No such file or directory gcc: fatal error: no input files compilation terminated. Cannot open file tmph5mvme.f gcc: error: tmph5mvme.c: No such file or directory gcc: fatal error: no input files compilation terminated. error: Command "/home/vital/Iraf/unix/hlib//f77.sh -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops -I/tmp/tmp_KRxI5/src.linux-x86_64-2.7 -I/usr/local/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c -c /tmp/tmph5mvme.f -o /tmp/tmp_KRxI5/tmp/tmph5mvme.o" failed with exit status 4 Traceback (most recent call last): File "/home/vital/git/pyResources/pyResources/Tutorials/test_f2py.py", line 42, in  import add ImportError: No module named add 

EDIT 2: lors de la modification de l’extension .f90 et de la définition de l’emplacement du compilateur fortran (à l’aide de whereis), l’erreur suivante apparaît:

 Gnu95FComstackr instance properties: comstack_switch = '-c' libraries = [] library_dirs = [] object_switch = '-o ' Fortran code C SUBROUTINE ZADD(A,B,C,N) C DOUBLE COMPLEX A(*) DOUBLE COMPLEX B(*) DOUBLE COMPLEX C(*) INTEGER N DO 20 J = 1, N C(J) = A(J)+B(J) 20 CONTINUE END Unknown vendor: "/usr/bin/gfortran" running build running config_cc unifing config_cc, config, build_clib, build_ext, build commands --comstackr options running config_fc unifing config_fc, config, build_clib, build_ext, build commands --fcomstackr options running build_src build_src building extension "add" sources f2py options: [] f2py:> /tmp/tmpHgqM4n/src.linux-x86_64-2.7/addmodule.c creating /tmp/tmpHgqM4n/src.linux-x86_64-2.7 Reading fortran codes... Reading file '/tmp/tmpiLO46g.f' (format:fix,ssortingct) Post-processing... Block: add Block: zadd Post-processing (stage 2)... Building modules... Building module "add"... Constructing wrapper function "zadd"... getarrdims:warning: assumed shape array, using 0 instead of '*' getarrdims:warning: assumed shape array, using 0 instead of '*' getarrdims:warning: assumed shape array, using 0 instead of '*' zadd(a,b,c,n) Wrote C/API module "add" to file "/tmp/tmpHgqM4n/src.linux-x86_64-2.7/addmodule.c" adding '/tmp/tmpHgqM4n/src.linux-x86_64-2.7/fortranobject.c' to sources. adding '/tmp/tmpHgqM4n/src.linux-x86_64-2.7' to include_dirs. copying /usr/local/lib/python2.7/dist-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmpHgqM4n/src.linux-x86_64-2.7 copying /usr/local/lib/python2.7/dist-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmpHgqM4n/src.linux-x86_64-2.7 build_src: building npy-pkg config files running build_ext customize UnixCComstackr customize UnixCComstackr using build_ext don't know how to comstack Fortran code on platform 'posix' with '/usr/bin/gfortran' comstackr. Supported comstackrs are: g95,compaq,none,intele,ibm,gnu,mips,lahey,nag,pathf95,intelem,gnu95,intelv,intelvem,absoft,intelev,pg,sun,hpux,vast,intel) warning: build_ext: f77_comstackr=/usr/bin/gfortran is not available. building 'add' extension error: extension 'add' has Fortran sources but no Fortran comstackr found 

EDIT 3:

En utilisant les arguments:

 f2py.comstack(sourcecode, modulename='add', extra_args = '-c --help-fcomstackr') 

Je reçois la sortie suivante:

 Gnu95FComstackr instance properties: archiver = ['/usr/bin/gfortran', '-cr'] comstack_switch = '-c' comstackr_f77 = ['/home/vital/Iraf/unix/hlib//f77.sh', '-Wall', '-g', '- ffixed-form', '-fno-second-underscore', '-fPIC', '-O3', '- funroll-loops'] comstackr_f90 = ['/usr/bin/gfortran', '-Wall', '-g', '-fno-second- underscore', '-fPIC', '-O3', '-funroll-loops'] comstackr_fix = ['/usr/bin/gfortran', '-Wall', '-g', '-ffixed-form', '- fno-second-underscore', '-Wall', '-g', '-fno-second- underscore', '-fPIC', '-O3', '-funroll-loops'] libraries = ['gfortran'] library_dirs = [] linker_exe = ['/usr/bin/gfortran', '-Wall', '-Wall'] linker_so = ['/usr/bin/gfortran', '-Wall', '-g', '-Wall', '-g', '- shared'] object_switch = '-o ' ranlib = ['/usr/bin/ranlib'] version = LooseVersion ('4.8') version_cmd = ['/usr/bin/gfortran', '-dumpversion'] Traceback (most recent call last): File "", line 1, in  File "/usr/local/lib/python2.7/dist-packages/numpy/f2py/f2py2e.py", line 648, in main run_comstack() File "/usr/local/lib/python2.7/dist-packages/numpy/f2py/f2py2e.py", line 633, in run_comstack setup(ext_modules=[ext]) File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/core.py", line 169, in setup return old_setup(**new_attr) File "/usr/lib/python2.7/distutils/core.py", line 137, in setup ok = dist.parse_command_line() File "/usr/lib/python2.7/distutils/dist.py", line 467, in parse_command_line args = self._parse_command_opts(parser, args) File "/usr/lib/python2.7/distutils/dist.py", line 576, in _parse_command_opts func() File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/command/config_comstackr.py", line 15, in show_fortran_comstackrs show_fcomstackrs(dist) File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcomstackr/__init__.py", line 889, in show_fcomstackrs c.customize(dist) File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcomstackr/__init__.py", line 502, in customize get_flags('opt', oflags) File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcomstackr/__init__.py", line 493, in get_flags flags.extend(getattr(self.flag_vars, tag)) File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/environment.py", line 39, in __getattr__ return self._get_var(name, conf_desc) File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/environment.py", line 53, in _get_var var = self._hook_handler(name, hook) File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcomstackr/__init__.py", line 700, in _environment_hook return hook() File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcomstackr/gnu.py", line 206, in get_flags_opt v = self.get_version() File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcomstackr/__init__.py", line 432, in get_version version = CComstackr.get_version(self, force=force, ok_status=ok_status) File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/ccomstackr.py", line 491, in CComstackr_get_version version = matcher(output) File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcomstackr/gnu.py", line 79, in version_match v = self.gnu_version_match(version_ssortingng) File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcomstackr/gnu.py", line 76, in gnu_version_match raise ValueError(err + version_ssortingng) ValueError: A valid Fortran version was not found in this ssortingng: invalid parameter -dumpversion