2012-04-19 93 views
1

我是Python的新手,希望能夠使用我現有的Fortran代碼並從Python運行它。我正在嘗試使用F2PY編譯一個小型的「Hello world」Fortran程序,方法是按照在線說明進行操作。F2PY返回ValueError:無效版本號'4'。

我正在使用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: invalid version number '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.' 

回答

2

mingw32的distutils插件使用不兼容的版本字符串。在的distutils/version.py,線100,版本字符串匹配正則表達式是

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

編輯文件mingw32ccompiler.py手動和變更線127閱讀

elif self.gcc_version < "4.0": 
+0

三江源以往那麼多了!這是做的伎倆,我現在很樂意編譯Fortran ..... – Alice 2012-04-23 10:22:01

+0

沒問題。你能將我的答案標記爲正確嗎? – bdforbes 2012-04-24 04:21:12