[f2py] Specific problems in using f2py on Windows for newbie

Pearu Peterson pearu at f2py.com
Wed Nov 16 21:55:56 EET 2011


Hi Jarrod,

I was able to reproduce the reported issues on a XP. To be sure that
we have similar setups, I'll describe first how to install the required
software (you
may have completed most, if not all, of the steps below).

Prerequisites
-------------------

1. Download EPD Free and run the installer. Btw, it seems that EPD Free has
disabled f2py scripting that numpy would have in a normal install. But
that's ok, below we'll have a workaround to this.

2. Download MinGW installer, and while running it, make sure that
the following items are checked:
  C++ compiler
  Fortran compiler
  MSYS Basic System
If you haven't checked Fortran compiler before, just rerun the MinGW
installer.

3. In principle, one should add 'c:\MinGW\bin' to Windows PATH environment
variable and restart the computer to make it effective. However, one may
skip this when using a f2py function that I'll define below.

Running f2py from IDLE
----------------------------------

1. Open IDLE and copy the following function definition to it:

def f2py(args):
   from numpy.distutils.exec_command import exec_command
   import os, sys
   mingw_path = 'C:\\MinGW\\bin'
   if mingw_path not in os.environ['PATH']:
       os.environ['PATH'] = os.environ['PATH'] + ';' + mingw_path
   s, o = exec_command([sys.executable, '-c', '"from numpy.f2py.f2py2e
import main; main()"', args], execute_in=os.getcwd())
   print o

2. Change to the working directory:

>>> import os
>>> os.chdir(r'c:\pydev')
>>> print os.getcwd()
c:\pydev

3. Check that f2py finds Fortran compilers:

>>> f2py('-c --help-fcompiler')
Found executable C:\Python27\python.exe
Gnu95FCompiler instance properties:
  archiver        = ['C:\\MinGW\\bin\\gfortran.exe', '-cr']
  compile_switch  = '-c'
  compiler_f77    = ['C:\\MinGW\\bin\\gfortran.exe', '-Wall', '-ffixed-
                    form', '-fno-second-underscore', '-O3',
'-funroll-loops']
  compiler_f90    = ['C:\\MinGW\\bin\\gfortran.exe', '-Wall', '-fno-second-
                    underscore', '-O3', '-funroll-loops']
  compiler_fix    = ['C:\\MinGW\\bin\\gfortran.exe', '-Wall', '-ffixed-
                    form', '-fno-second-underscore', '-Wall', '-fno-second-
                    underscore', '-O3', '-funroll-loops']
  libraries       = ['gfortran']
  library_dirs    = ['c:\\mingw\\lib\\gcc\\mingw32\\4.5.2']
  linker_exe      = ['C:\\MinGW\\bin\\gfortran.exe', '-Wall', '-Wall']
  linker_so       = ['C:\\MinGW\\bin\\gfortran.exe', '-Wall', '-Wall', '-
                    shared']
  object_switch   = '-o '
  ranlib          = ['C:\\MinGW\\bin\\gfortran.exe']
  version         = LooseVersion ('4.5.2')
  version_cmd     = ['C:\\MinGW\\bin\\gfortran.exe', '--version']
Fortran compilers found:
  --fcompiler=gnu95  GNU Fortran 95 compiler (4.5.2)
Compilers available for this platform, but not found:
  --fcompiler=absoft    Absoft Corp Fortran Compiler
  --fcompiler=compaqv   DIGITAL or Compaq Visual Fortran Compiler
  --fcompiler=g95       G95 Fortran Compiler
  --fcompiler=gnu       GNU Fortran 77 compiler
  --fcompiler=intelem   Intel Fortran Compiler for 64-bit apps
  --fcompiler=intelev   Intel Visual Fortran Compiler for Itanium apps
  --fcompiler=intelv    Intel Visual Fortran Compiler for 32-bit apps
  --fcompiler=intelvem  Intel Visual Fortran Compiler for 64-bit apps
Compilers not available on this platform:
  --fcompiler=compaq   Compaq Fortran Compiler
  --fcompiler=hpux     HP Fortran 90 Compiler
  --fcompiler=ibm      IBM XL Fortran Compiler
  --fcompiler=intel    Intel Fortran Compiler for 32-bit apps
  --fcompiler=intele   Intel Fortran Compiler for Itanium apps
  --fcompiler=lahey    Lahey/Fujitsu Fortran 95 Compiler
  --fcompiler=mips     MIPSpro Fortran Compiler
  --fcompiler=nag      NAGWare Fortran 95 Compiler
  --fcompiler=none     Fake Fortran compiler
  --fcompiler=pathf95  PathScale Fortran Compiler
  --fcompiler=pg       Portland Group Fortran Compiler
  --fcompiler=sun      Sun or Forte Fortran 95 Compiler
  --fcompiler=vast     Pacific-Sierra Research Fortran 90 Compiler
For compiler details, run 'config_fc --verbose' setup command.

4. To test f2py, download
http://cens.ioc.ee/projects/f2py2e/usersguide/fib3.f to c:\pydev\data\fib3.f
and run
>>> f2py('-c --compiler=mingw32 --fcompiler=gnu95 data\\fib3.f -m foo
--build-dir data\\temp')
running build
<snip..>
gfortran.exe:f77: data\fib3.f
C:\MinGW\bin\gfortran.exe -Wall -Wall -shared
data\temp\Release\data\temp\src.win32-2.7\foomodule.o
data\temp\Release\data\temp\src.win32-2.7\fortranobject.o
data\temp\Release\data\fib3.o -Lc:\mingw\lib\gcc\mingw32\4.5.2
-LC:\Python27\libs -LC:\Python27\PCbuild -lpython27 -lgfortran -o .\foo.pyd
running scons
>>>

5. Import the extension module. But first, its location must be added to
the sys.path:
>>> import sys
>>> sys.path.append(os.getcwd())
>>> import foo

6. Check the documentation of foo and run the Fortran code:
>>> print foo.__doc__
This module 'foo' is auto-generated with f2py (version:2).
Functions:
  a = fib(n)
.>>> print foo.fib(10)
[  0.   1.   1.   2.   3.   5.   8.  13.  21.  34.]

That's it!

Let me add few warning notes:

Due to the way Python imports extension modules (only once per session and
reload has no effect), in regenerating the extension modules, one has to
change the module names for each time. For example,

>>> f2py('-c --compiler=mingw32 --fcompiler=gnu95 data\\fib3.f -m foo1
--build-dir data\\temp')
>>> f2py('-c --compiler=mingw32 --fcompiler=gnu95 data\\fib3.f -m foo2
--build-dir data\\temp')
...

This is the downside of using IDLE (or any other Python interactive session
program) for calling and testing f2py generated extension modules
(actually, this applies to all extension modules under development).
A possible workaround (when you insists of using IDLE) is to have two IDLE
sessions opened: one for calling f2py and another for testing the extension
modules. Of course, when the extension module is regenerated, the other
IDLE session must be restarted.

HTH,
Pearu

On Wed, Nov 9, 2011 at 4:21 PM, Jarrod Wilcox <jarrodwilcox at verizon.net>wrote:

> Pearu et al
>
> Thank you for response to previous post.  My query now has more detail.
>
> I am running on Intel Core 2 64 bit Windows 7.  I reinstalled Python and
> numpy using the Enthought free distribution, with Python 2.7.2 (32 bit)
> July
> 3, 2011.
>
> To get C and Fortran compilers, I also installed MinGW using the
> prepackaged
> executable version 0.3-alpha-2.1.  It includes MSYS, although I did not use
> it in what follows.
>
> As a newbie, I am most comfortable running from IDLE rather than a terminal
> window.  An IDLE script follows:
>
> ----------------------------------------------------------------------------
> -------------
> import numpy
> from numpy import f2py
> import sys
>
> print("sys.path:")
> print(sys.path)
>
> def main():
>    print("Entering main()")
>    if '-c' in sys.argv[1:]:
>        f2py.f2py2e.run_compile()
>    else:
>        f2py.f2py2e.run_main(sys.argv[1:])
>
> print("RESULT OF: f2py -m fib1 -h fib1.pyf C:/pydev/data/fib1.f --build-dir
> C:/pydev/data/temp/")
> fcommand="f2py -m fib1 -h fib1.pyf C:/pydev/data/fib1.f --build-dir
> C:/pydev/data/temp/"
> sys.argv=fcommand.split()
> answer1=main()
> print(answer1)
>
> print(' ')
> print("RESULT OF: f2py -c --help-fcompiler")
> fcommand="f2py -c --help-fcompiler"
> sys.argv=fcommand.split()
> answer2=main()
> print(answer2)
>
> print(' ')
> print("RESULT OF: f2py -c c:/pydev/data/temp/fib1.pyf
> c:/pydev/data/fib1.f")
> fcommand="f2py -c c:/pydev/data/temp/fib1.pyf c:/pydev/data/fib1.f"
> sys.argv=fcommand.split()
> answer3=main()
> print(answer3)
>
> ----------------------------------------------------------------------------
> --------------------------
> THE OUTPUT OF RUNNING THE SCRIPT FOLLOWS, WITH ANNOTATIONS IN CAPS:
>
> ----------------------------------------------------------------------------
> ------------------------------
> Python 2.7.2 |EPD_free 7.1-2 (32-bit)| (default, Jul  3 2011, 15:13:59)
> [MSC
> v.1500 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
> >>> ================================ RESTART
> ================================
> >>>
> sys.path:
> ['C:\\pydev\\data', 'C:\\Python27\\Lib\\idlelib',
> 'C:\\Python27\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib',
> 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27',
> 'C:\\Python27\\lib\\site-packages',
> 'C:\\Python27\\lib\\site-packages\\PIL',
> 'c:\\MinGW\\bin', 'c:\\pydev\\data',
> 'C:\\Python27\\lib\\site-packages\\win32',
> 'C:\\Python27\\lib\\site-packages\\win32\\lib',
> 'C:\\Python27\\lib\\site-packages\\Pythonwin']
> -------------------------------------------------------------------------
> NOTE: SYS.PATH INCLUDES LOCATION OF MINGW AND GFORTRAN AND GCC COMPILERS
> --------------------------------------------------------------------------
> RESULT OF: f2py -m fib1 -h fib1.pyf C:/pydev/data/fib1.f --build-dir
> C:/pydev/data/temp/
> Entering main()
> Reading fortran codes...
>        Reading file 'C:/pydev/data/fib1.f' (format:fix,strict)
> Post-processing...
>        Block: fib1
>                        Block: fib
> Post-processing (stage 2)...
> Saving signatures to file "C:/pydev/data/temp/fib1.pyf"
> None
>
> ----------------------------------------------------------------------------
> ---------------
> NOTE: F2PY SEEMED TO WORK WELL IN PREPARING A WRAPPER FOR A SIMPLE FORTRAN
> SUBROUTINE ON FIBONNACI SERIES
> HOWEVER, NO FORTRAN COMPILER IS FOUND:
>
> ----------------------------------------------------------------------------
> --------------------
> RESULT OF: f2py -c --help-fcompiler
> Entering main()
> Fortran compilers found:
> Compilers available for this platform, but not found:
>  --fcompiler=absoft    Absoft Corp Fortran Compiler
>  --fcompiler=compaqv   DIGITAL or Compaq Visual Fortran Compiler
>  --fcompiler=g95       G95 Fortran Compiler
>  --fcompiler=gnu       GNU Fortran 77 compiler
>  --fcompiler=gnu95     GNU Fortran 95 compiler
>  --fcompiler=intelem   Intel Fortran Compiler for 64-bit apps
>  --fcompiler=intelev   Intel Visual Fortran Compiler for Itanium apps
>  --fcompiler=intelv    Intel Visual Fortran Compiler for 32-bit apps
>  --fcompiler=intelvem  Intel Visual Fortran Compiler for 64-bit apps
> Compilers not available on this platform:
>  --fcompiler=compaq   Compaq Fortran Compiler
>  --fcompiler=hpux     HP Fortran 90 Compiler
>  --fcompiler=ibm      IBM XL Fortran Compiler
>  --fcompiler=intel    Intel Fortran Compiler for 32-bit apps
>  --fcompiler=intele   Intel Fortran Compiler for Itanium apps
>  --fcompiler=lahey    Lahey/Fujitsu Fortran 95 Compiler
>  --fcompiler=mips     MIPSpro Fortran Compiler
>  --fcompiler=nag      NAGWare Fortran 95 Compiler
>  --fcompiler=none     Fake Fortran compiler
>  --fcompiler=pathf95  PathScale Fortran Compiler
>  --fcompiler=pg       Portland Group Fortran Compiler
>  --fcompiler=sun      Sun or Forte Fortran 95 Compiler
>  --fcompiler=vast     Pacific-Sierra Research Fortran 90 Compiler
> For compiler details, run 'config_fc --verbose' setup command.
> None
>
> ----------------------------------------------------------------------------
> ----------
> NOTE:  GCC MAY ALSO NOT BE FOUND.  AND THERE MAY BE ADDITIONAL PROBLEMS.
>
> ----------------------------------------------------------------------------
> ----------
> RESULT OF: f2py -c c:/pydev/data/temp/fib1.pyf c:/pydev/data/fib1.f
> Entering main()
> 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 "fib1" sources
> creating c:c:
>
> Traceback (most recent call last):
>  File "C:\pydev\data\fib1test.py", line 37, in <module>
>    answer3=main()
>  File "C:\pydev\data\fib1test.py", line 16, in main
>    f2py.f2py2e.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 169, in setup
>    raise SystemExit, "error: " + str(msg)
> SystemExit: error: could not create 'c:c:': The filename, directory name,
> or
> volume label syntax is incorrect
> >>>
>
> ----------------------------------------------------------------------------
> --------------------------------
> NOTE:  I MUST BE MISSING SOMETHING OBVIOUS.  IF YOU OR ANYONE ELSE HAS
> ADVICE, PLEASE ASSUME I HAVE ONLY VERY SLIGHT FAMILIARITY WITH C AND UNIX.
>
> ----------------------------------------------------------------------------
> -------------------------------------
> Thanks,
> Jarrod
>
>
> _______________________________________________
> f2py-users mailing list
> f2py-users at cens.ioc.ee
> http://cens.ioc.ee/mailman/listinfo/f2py-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://cens.ioc.ee/pipermail/f2py-users/attachments/20111116/a08c3816/attachment.htm 


More information about the f2py-users mailing list