Python ImportError using F2py - python

I have two F90 files that I want to use with python, so I'm using f2py to compile them (together) and result a python module, I'm doing it like this:
f2py -c ControlParameters.F90 vector.F90 -m test
When I do this, everything is fine, and I can use functions and subroutines from those files with python.
But now, I need to use f2py with those two files, adding a couple of libraries, like -liomp5 or -lzmumps, I get the python module like in the upper case, but when I try to import it from python I have the following error:
ImportError: /opt/intel/composer_xe_2013.1.117/mkl/lib/intel64/libmkl_blacs_intelmpi_lp64.so: undefined symbol: MPI_Finalize
There are some libraries that cannot be used with f2py?

Related

Python gives error when importing simple C extension module

On windows I have built a very simple "hello world" C extension (the file hello.c from this site https://gist.github.com/physacco/2e1b52415f3a964ad2a542a99bebed8f). Using VS2015 I successfully obtain hello.dll. The problem is that I can't figure out how to import this file/module.
In the python shell (python 3.7) I have made sure that I'm in the same folder as the hello.dll. I have also made sure that sys.path() contains the folder path. But when I write "import hello" I get an error "ModuleNotFoundError: No module named 'hello'"
Does anyone has an idea of what is wrong is this very simple setup?
Update:
When trying to import a module that does not exist the ModuleNotFoundError is reported. After renaming the hello.dll to hello.pyd an ImportError is returned. So it seems like it tries to actually load the module.
Python compiled modules on Windows have the extension .pyd, not .dll. If you'd built it using setup.py the file would be built with the correct name. However, you built it yourself and gave it a name that Python doesn't recognise as a module.
In terms of the build command: you have to link it with libpython. You don't look to be doing this. The error you report is definitely one that you can get if the module is not linked against all its dependencies.
I know you don't want to use setup.py, however I'd use it at least once just to see what it does and if it works. You'll then at least have a command that you can copy with a working set of options.

Compiling IronPython into an exe that uses standard library packages

In my IronPython script, I'm using standard libary modules like ConfigParser, logging and JSON.
Then I use pyc.py to create an executable. At first I ran into problems, namely '...ImportException: no module named ...'
since they weren't being included in the exe and accompanying dlls.
So I ran a solution from here: IronPython: EXE compiled using pyc.py cannot import module "os" and it mostly worked.
For example, importing 'ConfigParser' would work since in the IronPython 'Lib' folder as a module, it's there as 'ConfigParser.py'. However I'm still having trouble using JSON and logging since they're inside of folders with their name (packages?).
I'm feeling that I'm just missing something simple, and probably need to read up more on python modules and how they really work, but I'm not sure what I should be looking for.
Any help would be greatly appreciated.
Thanks!
EDIT:
I can't answer my own question yet, so I'll leave this here.
Somehow got it to work in a really 'hacky' way. There must be another much cleaner solution to this that I'm missing (some option in pyc.py?)
Here's what I did:
1) Made the StdLib.dll file generated from the link above (IronPython: EXE compiled using pyc.py cannot import module "os"). This would be missing the std lib packages.
2) Used SharpDevelop to compile the standard lib packages that weren't included in the above dll following the method here: http://community.sharpdevelop.net/blogs/mattward/archive/2010/03/16/CompilingPythonPackagesWithIronPython.aspx
3) Used SharpDevelop to build my program and tie together all the references.
- Reference to the dlls made in step 2
- Reference to the StdLib.dll made in step 1
Again, there must be a better solution to this.
I've found two ways to compile standard library python packages:
1st way: Individually compile each package into a dll
Using pyc.py, run something like (this example compiles logging package):
ipy pyc.py ".\Lib\logging\__init__.py" ".\Lib\logging\config.py" ".\Lib\logging\handlers.py" /target:dll /out:logging
This creates a logging.dll file, which you can then use like this:
import clr
clr.AddReference('StdLib') #from the compilation of non-package std libraries
clr.AddReference('logging')
import logging
**Note: This is assuming you've run the solution from IronPython: EXE compiled using pyc.py cannot import module "os" to create StdLib.dll
2nd way: Modify the compilation script that generated StdLib.dll
I changed this line:
#Build StdLib.DLL
gb = glob.glob(r".\Lib\*.py")
gb.append("/out:StdLib")
To this:
#Build StdLib.DLL
gb1 = glob.glob(r".\Lib\*.py")
gb2 = glob.glob(r".\Lib\*\*.py")
gb3 = glob.glob(r".\Lib\*\*\*.py")
gb = list(set(gb1 + gb2 + gb3))
gb.append("/out:StdLib")
This includes the subfolders in the Lib directory which get missed in the original regex (only modules get included). Now, packages like xml, json, logging, etc. get included into StdLib.dll

F2PY doesn't find a module

I am having difficulty with getting a f2py compiled module work in Python.
I have a piece of software written in Fortran that compiles well on a Linux 64bit machine.
Further on F2Py compiles a Python module that uses Fortran bits as well.
Here is how the Python module is compiled:
f2py --fcompiler=gfortran -I"path-to-dir-with-mod-files" -c -m mod_landems mod_landem.f90
But once I want to import that module I get an error (in Ipython):
----> 1 import mod_landems
ImportError: ./mod_landems.so: undefined symbol: __nesdis_landem_module_MOD_nesdis_landem
To be honest I am confused with this error. Search did not help much so I need to ask you here: how can I possibly make it work? If I put the python module code in the same directory as where the mod files are it produces same error message.
Here is a piece of my (primitive) code:
module n_landem
implicit none
! INPUT VARIABLES
real(8) Angle
real(8) Sm_Content
real(8) Veg_Frac
real(8) Soil_Temp
real(8) Land_Temp
real(8) Snow_Depth
real(8) Frequency
! OUTPUT VARIABLES
real(8) Emis_H
real(8) Emis_V
contains
subroutine landem
USE NESDIS_LANDEM_MODULE
USE TYPE_KINDS, ONLY : fp
call NESDIS_LandEM(Angle,Frequency,Sm_Content,Veg_Frac,Soil_Temp,Land_Temp,Snow_Depth,Emis_H,Emis_V)
end subroutine landem
end module n_landem
If I recall correctly some time ago this module was importable, but can't seem to make it work on either debian64 installation or debian32bit computer.
in your f2py call, you have to pass the libraries you are linking explicitly with '-l', the same you would pass it to your Fortran compiler (i.e. gfortran). Therefore, does
f2py -c --fcompiler=gfortran -I"path-to-dir-with-mod-files" --fcompiler=gfortran -I"path-to-dir-with-mod-files" -lNESDIS_LandEM_Module -m mod_landems mod_landem.f90 -m mod_landems mod_landem.f90
work for you ?
Best,
Max.
I'm working on a similar project and too am new to Fortran. Using the reference below I found that you import the fortran module just as you would import a library and then call a function similarly.
http://cens.ioc.ee/projects/f2py2e/usersguide/#the-quick-and-smart-way

Python failing to compile on Linux with custom module built in

I am trying to compile Python 2.6.7 with a custom module built in but I am running into problems. Everything is going fine until the linker tries to link the dependencies that my module has. The make file gets generated just fine and the path were the libraries are installed to is correct. The way I configured Python to compile with my custom module is like so:
_myplugin _myplugin.c++ -I/home/me/Desktop/Depends/include -L/home/me/Desktop/Depends/lib -l libcrvs_pic.a -l libsb_pic.a -l liblmgr_dongle_stup_pic.a
The build fails on this line:
/usr/bin/ld: cannot find -llibcrvs_pic.a
Any help would be much appreciated!
I think what you need to do is specify the libraries without the lib prefix, and also without the .a suffix.
Like so:
-lcrvs_pic

Issues when using f2py module in python code

I have a FORTRAN code that required the following compile command
gfortran -c interp.f -ffixed-format -ffix-line-length-none
I compiled the same using f2py module in python
from numpy import f2py
f2py.compile(open('interp.f').read(),modulename='interp',extra_flags='-ffixed-format -ffix-line-length-none',verbose=0)
It is unable to compile the module. It gives an error saying invalid file format '' at '-ffized-format'
Please help
Neither -ffixed-format, nor -ffix-line-length-none seem to be valid gfortran options. Consider using -ffixed-form and -ffixed-line-length-none instead.

Categories