【问题标题】:Setting the fortran compiler in f2py在 f2py 中设置 fortran 编译器
【发布时间】:2016-03-12 00:20:05
【问题描述】:

我正在尝试运行 f2py example 来创建已编译的扩展模块¶:

# import os
# os.environ["CC"] = "gcc" 
# os.environ["CXX"] = "g++"

# Using post-0.2.2 scipy_distutils to display fortran compilers
from scipy_distutils.fcompiler import new_fcompiler
compiler = new_fcompiler() # or new_fcompiler(compiler='intel')
compiler.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.compile(sourcecode, modulename='add', extra_args = '--compiler=gnu --fcompiler=gnu')
f2py.compile(sourcecode, modulename='add', extra_args = '--fcompiler=gfortran')
# f2py.compile(sourcecode, modulename='add')

import add

但是,代码不起作用。我相信这是因为它没有使用安装的编译器(操作系统是 ubuntu 12.04)。但是,尽管使用了“--fcompile”参数或更改了“CC”和“CXX”条目,它仍然无法正常工作......

这是完整的错误日志。请提供任何建议:

Could not locate executable g77
Could not locate executable f77
customize GnuFCompiler
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 IntelFCompiler
customize LaheyFCompiler
customize PGroupFCompiler
customize AbsoftFCompiler
customize NAGFCompiler
customize VastFCompiler
customize GnuFCompiler
customize CompaqFCompiler
customize IntelItaniumFCompiler
customize G95FCompiler
customize GnuFCompiler
GnuFCompiler instance properties:
  archiver        = ['ar', '-cr']
  compile_switch  = '-c'
  compiler_f77    = ['f77', '-Wall', '-fno-second-underscore']
  compiler_f90    = None
  compiler_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 --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler 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,strict)
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 UnixCCompiler
customize UnixCCompiler using build_ext
customize Gnu95FCompiler
Found executable /usr/bin/gfortran
Found executable /home/vital/Iraf/unix/hlib/f77.sh
Found executable /usr/bin/ranlib
customize Gnu95FCompiler using build_ext
building 'add' extension
compiling C sources
C compiler: gfortran -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC

creating /tmp/tmpNKECn3/tmp
creating /tmp/tmpNKECn3/tmp/tmpNKECn3
creating /tmp/tmpNKECn3/tmp/tmpNKECn3/src.linux-x86_64-2.7
compile 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 compiler: /home/vital/Iraf/unix/hlib//f77.sh -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran f90 compiler: /usr/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran fix compiler: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
compile 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 <module>
    import add
ImportError: No module named add

附: Iraf 是天文软件,不是编译器。

编辑 1:

按照 jthomas 的建议使用正确的导入,我得到一个较短的错误,但它仍在尝试使用 IRAF 文件夹中的脚本。这是使用该代码的完整错误消息:

Gnu95FCompiler instance properties:
  compile_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 --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler 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,strict)
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 UnixCCompiler
customize UnixCCompiler using build_ext
customize Gnu95FCompiler
Found executable /usr/bin/gfortran
Found executable /home/vital/Iraf/unix/hlib/f77.sh
Found executable /usr/bin/ranlib
customize Gnu95FCompiler using build_ext
building 'add' extension
compiling C sources
C compiler: gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-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
compile 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 compiler: /home/vital/Iraf/unix/hlib//f77.sh -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran f90 compiler: /usr/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran fix compiler: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
compile 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 <module>
    import add
ImportError: No module named add

编辑 2:将扩展名更改为 .f90 并定义 fortran 编译器位置(使用 whereis)时,出现以下错误:

Gnu95FCompiler instance properties:
  compile_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 --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler 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,strict)
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 UnixCCompiler
customize UnixCCompiler using build_ext
don't know how to compile Fortran code on platform 'posix' with '/usr/bin/gfortran' compiler. Supported compilers 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_compiler=/usr/bin/gfortran is not available.

building 'add' extension
error: extension 'add' has Fortran sources but no Fortran compiler found

编辑 3:

使用参数:

f2py.compile(sourcecode, modulename='add', extra_args = '-c --help-fcompiler')

我得到以下输出:

Gnu95FCompiler instance properties:
  archiver        = ['/usr/bin/gfortran', '-cr']
  compile_switch  = '-c'
  compiler_f77    = ['/home/vital/Iraf/unix/hlib//f77.sh', '-Wall', '-g', '-
                    ffixed-form', '-fno-second-underscore', '-fPIC', '-O3', '-
                    funroll-loops']
  compiler_f90    = ['/usr/bin/gfortran', '-Wall', '-g', '-fno-second-
                    underscore', '-fPIC', '-O3', '-funroll-loops']
  compiler_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 "<string>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/numpy/f2py/f2py2e.py", line 648, in main
    run_compile()
  File "/usr/local/lib/python2.7/dist-packages/numpy/f2py/f2py2e.py", line 633, in run_compile
    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_compiler.py", line 15, in show_fortran_compilers
    show_fcompilers(dist)
  File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcompiler/__init__.py", line 889, in show_fcompilers
    c.customize(dist)
  File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcompiler/__init__.py", line 502, in customize
    get_flags('opt', oflags)
  File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcompiler/__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/fcompiler/__init__.py", line 700, in _environment_hook
    return hook()
  File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcompiler/gnu.py", line 206, in get_flags_opt
    v = self.get_version()
  File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcompiler/__init__.py", line 432, in get_version
    version = CCompiler.get_version(self, force=force, ok_status=ok_status)
  File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/ccompiler.py", line 491, in CCompiler_get_version
    version = matcher(output)
  File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcompiler/gnu.py", line 79, in version_match
    v = self.gnu_version_match(version_string)
  File "/usr/local/lib/python2.7/dist-packages/numpy/distutils/fcompiler/gnu.py", line 76, in gnu_version_match
    raise ValueError(err + version_string)
ValueError: A valid Fortran version was not found in this string:
invalid parameter -dumpversion

【问题讨论】:

  • f2py -c --help-fcompiler 返回什么?查找“找到 Fortran 编译器”。
  • @unutbu 感谢您的关注我在编辑 3 中添加了结果
  • 从命令行运行f2py -c --help-fcompiler会得到什么?是否有必要从 Python 脚本中运行 f2py ?或者您只是想以任何方式编译add 模块?
  • @unutbu 再次感谢:我可以将 f2py 作为 /usr/bin/f2py -c -m add add.f90 运行。这会生成 python 至少可以“导入添加”的 add.so。如果需要,我可以使用 popen 运行此命令。如果您添加回复,我可以将其设置为已接受

标签: python c compilation fortran f2py


【解决方案1】:

根据tutorial docs,使用 f2py 的一种简单方法是从命令行运行

f2py -c -m add add.f

这将创建模块文件add.so(或add.pyd,取决于您的操作系统),可以将其导入到Python脚本中

import add

如果add.so 位于sys.path 中列出的目录中。

【讨论】:

    【解决方案2】:

    f2py 现在作为 numpy 的一部分进行维护。改变你的例子如下编译就好了。

    # import os
    # os.environ["CC"] = "gcc"
    # os.environ["CXX"] = "g++"
    
    # Using post-0.2.2 scipy_distutils to display fortran compilers
    from numpy.distutils.fcompiler import new_fcompiler
    compiler = new_fcompiler() # or new_fcompiler(compiler='intel')
    compiler.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.compile(sourcecode, modulename='add', extra_args = '--compiler=gnu --fcompiler=g$
    f2py.compile(sourcecode, modulename='add', extra_args = '--fcompiler=gfortran')
    # f2py.compile(sourcecode, modulename='add')
    
    import add
    

    【讨论】:

    • 非常感谢您的回复:我使用了您的导入,但仍然遇到非常相似的错误...它正在尝试使用 IRAF 脚本作为编译器。请问您有什么建议吗?我在问题中包含了新的错误消息
    • 我从未使用过 IRAF,但从您的错误消息和网上查看,它似乎包含一个 Fortran 77 编译器,并且您的路径指向该编译器以编译 Fortran 77。因为您的程序可以使用更高版本的 Fortran 编译,尝试将 add.f 的扩展名从 .f 更改为 .f90。您将需要更改文件本身的扩展名以及 .py 文件中的引用。此外,为了使您编译的程序可用,我认为您需要对其进行一些更改,但也许在它至少可以编译之后在另一个问题中更好地解决。
    • 再次感谢您的宝贵时间...我已经尝试了您的代码,但使用 f90 扩展名,我仍然遇到相同的错误...当我尝试提供编译器的完整地址时,有趣的是: --fcompiler = /usr/bin/gfortran 我收到错误:“不知道如何使用 '/usr/bin/gfortran' 编译器在平台 'posix' 上编译 Fortran 代码。” “警告:build_ext: f77_compiler=/usr/bin/gfortran 不可用。”
    • 正如@unutbu 所发布的,您可以尝试替代(更简单)的编译方法。他的回答是我通常使用的方法。我应该早点指出这一点。
    • 这是问题的间接解决方案,但感谢您和@unutbu,我至少可以运行 f2py。再次感谢 jthomas
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    相关资源
    最近更新 更多