F2PY返回ValueError:无效的版本号'4'

时间:2012-04-19 15:33:05

标签: python mingw fortran python-2.7 f2py

我是Python的新手,希望能够使用我现有的Fortran代码并从Python运行它。我正在尝试使用F2PY编译一个小的“Hello world”Fortran程序,按照在线说明进行操作。

我正在使用Windows。到目前为止,我已经安装了Python 2.7.3,NumPy 1.6.1,SciPy 10.1和MinGW 4.8。

然后我尝试了(除其他事项外)

f2py.py -c --compiler=mingw32 --fcompiler=gnu95 -m foo foo.f90

报告版本号无效。

  

ValueError:版本号无效'4。'

如果有人能够建议出现问题以及我需要改变什么以使其运行,我将非常感激。 F2PY的全部输出如下:

D:\python\fortran2>f2py.py -c --compiler=mingw32 --fcompiler=gnu95 -m foo foo.f90
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 "foo" sources
f2py options: []
f2py:> c:\docume~1\ajr\locals~1\temp\tmpwvnr2c\src.win32-2.7\foomodule.c
creating c:\docume~1\ajr\locals~1\temp\tmpwvnr2c
creating c:\docume~1\ajr\locals~1\temp\tmpwvnr2c\src.win32-2.7
Reading fortran codes...
        Reading file 'foo.f90' (format:free)
Post-processing...
        Block: foo
                        Block: hello
Post-processing (stage 2)...
Building modules...
        Building module "foo"...
                Constructing wrapper function "hello"...
                  hello()
        Wrote C/API module "foo" to file "c:\docume~1\ajr\locals~1\temp\tmpwvnr2c\src.win32-2.7\foomodule.c"
  adding 'c:\docume~1\ajr\locals~1\temp\tmpwvnr2c\src.win32-2.7\fortranobject.c' to sources.
adding 'c:\docume~1\ajr\locals~1\temp\tmpwvnr2c\src.win32-2.7' to include_dirs.
copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.c -> c:\docume~1\ajr\locals~1\temp\tmpwvnr2c\src.win32-2.7
copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.h -> c:\docume~1\ajr\locals~1\temp\tmpwvnr2c\src.win32-2.7
build_src: building npy-pkg config files
running build_ext
Traceback (most recent call last):
  File "c:\python27\scripts\f2py.py", line 24, in <module>
    main()
  File "C:\Python27\lib\site-packages\numpy\f2py\f2py2e.py", line 588, in main
    run_compile()
  File "C:\Python27\lib\site-packages\numpy\f2py\f2py2e.py", line 574, in run_compile
    setup(ext_modules = [ext])
  File "C:\Python27\lib\site-packages\numpy\distutils\core.py", line 186, in setup
    return old_setup(**new_attr)
  File "C:\Python27\lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Python27\lib\site-packages\numpy\distutils\command\build.py", line 37, in run
    old_build.run(self)
  File "C:\Python27\lib\distutils\command\build.py", line 127, in run
 self.run_command(cmd_name)
  File "C:\Python27\lib\distutils\cmd.py", line 326, in run_command
    self.distribution.run_command(command)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Python27\lib\site-packages\numpy\distutils\command\build_ext.py", line 92, in run
    force=self.force)
  File "C:\Python27\lib\site-packages\numpy\distutils\ccompiler.py", line 556, in new_compiler
    compiler = klass(None, dry_run, force)
  File "C:\Python27\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 127, in __init__
    elif self.gcc_version < "4.":
  File "C:\Python27\lib\distutils\version.py", line 138, in __cmp__
    other = StrictVersion(other)
  File "C:\Python27\lib\distutils\version.py", line 40, in __init__
    self.parse(vstring)
  File "C:\Python27\lib\distutils\version.py", line 107, in parse
    raise ValueError, "invalid version number '%s'" % vstring
ValueError: invalid version number '4.'

1 个答案:

答案 0 :(得分:2)

mingw32的distutils插件使用不兼容的版本字符串。在distutils / version.py第100行中,匹配正则表达式的版本字符串是

r'^(\d+) \. (\d+) (\. (\d+))? ([ab](\d+))?$'

手动编辑文件mingw32ccompiler.py并将第127行更改为

elif self.gcc_version < "4.0":