How to execute `f2py`? - python

How can I wrap in the f2py module?
I mean, I am reading a few tutorials that say I should execute
f2py FIB1.f -m FIB2 -h FIB1.pyf
However, I don't know where I have to execute that, for sure not in spyder or I am doing something wrong.
Why?
Because I execute this code that should create the extension module of Fortran with Python from my subroutine in Fortran, however an error is generated.
my Fortran subroutine:
SUBROUTINE FIB(A,N)
INTEGER N
REAL*8 A(N)
DO I=1,N
IF (I.EQ.1) THEN
A(I) = 0.0D0
ELSEIF (I.EQ.2) THEN
A(I) = 1.0D0
ELSE
A(I) = A(I-1) + A(I-2)
ENDIF
ENDDO
END
What I'm executing in Python:
import numpy.f2py as f2py
f2py FIB1.f -m FIB2 -h FIB1.pyf
The error is this one:
runfile('F:/SLB/Larryf2py/teste.py', wdir='F:/SLB/Larryf2py')
File "F:/SLB/Larryf2py/teste.py", line 9
f2py FIB1.f -m FIB2 -h FIB1.pyf
^
SyntaxError: invalid syntax
As far as I know, not sure, It should generate something like:
# File setup.py
def configuration(parent_package='',top_path=None):
from numpy.distutils.misc_util import Configuration
config = Configuration('',parent_package,top_path)
config.add_extension('m',
sources = ['m.pyf','foo.c'])
return config
if __name__ == "__main__":
from numpy.distutils.core import setup
setup(**configuration(top_path='').todict())
This example of what is generated is to C but I think its something like that to Fortran too.
What I think? That I should run the first code in another place of the Python...
I tried to reproduce this.

Are you adding f2py command inside your Python code? If yes, that's not good.
The line f2py FIB1.f -m FIB2 -h FIB1.pyf needs to be in command line, not inside any *.py script.
From F2PY Users Guide and Reference Manual
f2py is a program/compiler from The purpose of the F2PY –Fortran to Python interface generator– project is to provide a connection between Python and Fortran languages. F2PY is a Python package (with a command line tool f2py and a module f2py2e) that facilitates creating/building Python C/API extension modules that make it possible.
In additon, here is detailed explanation on how to use f2py .
There might be some other problems in OP's question but for the moment most vital is this one. Like the Fortran subrotuine is not using implicit none, etc.

Well I found an answer.
Looks like in this version of anaconda one is supossed tu put like
Python c:\user\anaconda3\scripts\f2py.py FIB1.f -m FIB2 -h FIB1.py
so this way that f2py.py part was substituted for all of that. For sure I'll have more trouble in the future with this module but so far my doubts are clear.

If you want to convert fortran into a python object using a python code, the following will work:
from numpy import f2py
with open('path_to_fotran_code') as sourcefile:
sourcecode = sourcefile.read()
f2py.compile(sourcecode, modulename='test_module', verbose=1,
extra_args= '--verbose'
'--compiler=mingw32')
import test_module
In case you do not have mingw32 you can use --compile=msvc (I ran into problems trying to use msvc which I could never solve with all the internet help).
Also ensure that your windows path environment is configured to point the fortran compiler path.

f2py is not a Python command, you cannot execute it in the Python shell or inside a .py source file. It is an executable command. You must execute it in your system's shell.
You still did not answer which operating system you have, but if it is Windows, you must run it in the CMD.exe command prompt or in PowerShell. If it is Linux or similar, run it in bash or similar shell. You must run it in the same directory (folder), where the Fortran source file is located.

Related

cannot insert breakpoint x in shared object library , (python using .so library from c++)

I'm trying to analyze c++ which is made into a .so file using pybind11.
The .so file has python API so the main python program uses the python API and calls functions in the .so file. I use two debugger for python (pdb) and c++ (gdb).
When I set breakpoint in the gdb, I get this error :
Cannot insert breakpoint 1.
Cannot access memory at address 0xb41798
when I set breakpoint in c++, there is no complaint, but when the program resumes in python, I have this error message in c++ debug window. How can I solve this problem?
ADD : here is how I run the debuggers .
for pdb : python3 -m pdb test.py for gdb : ddd /usr/loca/.../dlib...gnu.so 10498 where 10498 was the process id of the python program
I made the .so file (dlib c++ to .so file containing python API) like this :
sudo python3 setup.py install --clean --set USE_AVX_INSTRUCTIONS=1 --set DLIB_USE_CUDA=1 --set CUDA_NVCC_FLAGS="--expt-relaxed-constexpr" --compiler-flags "-O0 -g"
the setup.cfg file has
[build_ext]
debug = 1
maybe a problem in the way I made the .so file in?
for gdb : ddd /usr/loca/.../dlib...gnu.so 10498
Your DDD invocation is incorrect: the process 10498 is not running your dlib...gnu.so binary, it is running python3 binary with the .so loaded into it.
You need to invoke it like so: ddd python3 10498.

python command line options to a compiled python file (py2exe)

I, like many python programmers find the command line arguments for python described here very useful. specifically "-i" which keeps the python interpreter running after a program finishes for a stack trace. How can I use these with an exe file compiled with py2exe? Not to be confused with regular argument parsing in an exe file. If you came looking for that, find it here.
My first thought was to try:
pyprogram.exe -i -foo -bar
but that didn't work.
it should be noted that
pyprogram.exe -foo -bar
does in fact work for me.
what I am looking for is the .exe equivalent of
python -i pyprogram.py foo bar
Failing to find an implementation that works for all of the python command line options, what could I do just to make the "-i" argument work? it is the most important to have as an option in my executable.
I did not find anything on the py2exe wiki about passing arguments like -i (to enter interactive mode after execution).
One might be able to discover information about the argument handling in the py2exe source files.
Update: It indeed looks like py2exe does not handle any command line options like the normal interpreter does, instead it just passes them to the script. But it does handle the respective environment variable, which can be used as shown below.
However, as a workaround, you could try to set the PYTHONINSPECT Environment variable:
If this is set to a non-empty string it is equivalent to specifying the -i option.
E.g. run set PYTHONINSPECT=TRUE before running the program.
But, probably even better, this can be done from within the Python script:
This variable can also be modified by Python code using os.environ to force inspect mode on program termination.
Here's a little test script for os.environ (also os.putenv):
import os
one = os.environ
os.putenv("PYTHONINSPECT", "TRUE")
two = os.environ
os.environ["PYTHONINSPECT"] = "TRUE"
three = os.environ
print(one)
print(two)
print(three)
print( set(one.items()) ^ set(two.items()) )
print( set(one.items()) ^ set(three.items()) )
The behaviour is a little weird: there does not seem to be a difference, and it seems to only last until you exit the interactive mode:
G:\>py test.py > test.txt
>>> exit()
G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not defined
The contents of test.txt are:
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
set()
set()
But it seems to work either way (double check the documentation for yourself to ensure you are not corrupting your environment variables), so you could even implement an -i argument for yourself like:
import sys, os
if len(sys.argv) > 1 and sys.argv[1] == '-i':
os.putenv("PYTHONINSPECT", "TRUE")
#os.environ["PYTHONINSPECT"] = "TRUE"
print("interactive")
else:
print("normal")
which runs as follows
G:\>py test.py
normal
G:\>py test.py -i
interactive
>>> quit()
G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not defined
Trying with py2exe and Python 3.4.3 (newer versions are apparently not supported and you get an IndexError):
setup.py:
from distutils.core import setup
import py2exe
setup(console=['test.py'])
Get py2exe
G:\>c:\Python34\Scripts\pip.exe install py2exe
You are using pip version 6.0.8, however version 10.0.0b2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting py2exe
Using cached py2exe-0.9.2.2-py33.py34-none-any.whl
Installing collected packages: py2exe
Successfully installed py2exe-0.9.2.2
Run py2exe
G:\>c:\Python34\python.exe setup.py py2exe
running py2exe
3 missing Modules
------------------
? readline imported from cmd, code, pdb
? win32api imported from platform
? win32con imported from platform
Building 'dist\test.exe'.
Building shared code archive 'dist\library.zip'.
Copy c:\windows\system32\python34.dll to dist
Copy c:\Python34\DLLs\_hashlib.pyd to dist\_hashlib.pyd
Copy c:\Python34\DLLs\pyexpat.pyd to dist\pyexpat.pyd
Copy c:\Python34\DLLs\select.pyd to dist\select.pyd
Copy c:\Python34\DLLs\unicodedata.pyd to dist\unicodedata.pyd
Copy c:\Python34\DLLs\_ctypes.pyd to dist\_ctypes.pyd
Copy c:\Python34\DLLs\_socket.pyd to dist\_socket.pyd
Copy c:\Python34\DLLs\_lzma.pyd to dist\_lzma.pyd
Copy c:\Python34\DLLs\_ssl.pyd to dist\_ssl.pyd
Copy c:\Python34\DLLs\_bz2.pyd to dist\_bz2.pyd
Test
G:\>dist\test.exe
normal
G:\>dist\test.exe -i
interactive
>>> sys.exit()
Does not seem to have changed the environment variables permanently:
G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not defined
Also works with single exe:
from distutils.core import setup
import py2exe
setup(
options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
console = [{'script': "test.py"}],
zipfile = None,
)

F2py with Openmp gives import error in Python

I am able to compile the minimal working example below in Fortran which uses Openmp and run to give the expected result (prints 1).
subroutine test
use omp_lib
write(*,*) omp_get_num_threads()
end subroutine
However using this in python with f2py gives the error:
ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.
I have used the dependency walker to test if the issue is in linking to the openmp dll, however the following .dll is linked in both the fortran executable and the .pyd compiled from f2py:
c:\tdm-gcc-64\bin\LIBGOMP_64-1.DLL
Therefore I am confused as to why python would fail to load the .dll, as it appears to be linked correctly from f2py. The f2py command that I'm using to generate the .pyd is
python -m numpy.f2py -c -m %output% %input%.f90 --fcompiler=gnu95 --compiler=mingw32 --f90flags="-fopenmp " -lgomp
Any help would be greatly appreciated, thanks.
EDIT: I have tested this with another windows PC with the same installation setup, and get the same error message. Am I missing something?
EDIT 2: Apparently this program wouldn't actually work in f2py, so is bad example. My apologies. I'm actually working with subroutines, which are able to work with f2py correctly so long as no openmp commands are present.
EDIT 3: I have replaced the example code to be a subroutine instead of program due to feedback from Pierre de Buyl, though this makes no difference to my question.
This works for me:
tesf.f95
subroutine nthreads
!$ use omp_lib
integer :: nt
nt = 0
!$ nt = omp_get_max_threads()
write(*,*) 'Nthreads'
write(*,*) nt
end subroutine
Compile with:
f2py -c test.f95 -m test --f90flags='-fopenmp' -lgomp -lpthread --compiler=mingw32 --fcompiler=gfortran
If I run:
python -c "import test; test.nthreads()"
The result is:
Nthreads
8
It appears that the problem was in using tdm-gcc-64 as a compiler, and so I am instead using a mingw64 installation which works as expected.

make: f2py: No such file or directory

I am running Mac OS X 10.10. I have some python code I have inherited. I need to run "make" in a certain directory, because I get a warning when I run my python script along the lines of WARNING: failed to import test1lib. Please run make in /this/directory/ So I go to that directory where I find the following files:
Makefile __init__.py __init__.pyc foo.py foo.pyc foo_code.f foolib.f
So I run $ make
but I get the following error:
f2py -c foolib.f -m foolib
make: f2py: No such file or directory
make: *** [foolib.so] Error 1
Running which f2py returns /usr/local/bin/f2py so its not like I don't have f2py, but why is my terminal expecting f2py to be a directory? What can I do so that I can run make on this case?
If you have f2py, then it's either not on the search PATH, or it has not been made executable.
I don't know if you are on Linux or Windows. Try to execute f2py manually and see if it starts. If not, try to locate it, and see where it is installed.
If you can start it from the command line, then show us the Makefile around where the command f2py is located.
EDIT: Ok... I'm suspecting that f2py is actually a Python script, and it has a shebang line (first line starts with #!) which calls for a python version you don't have. In my system, f2py start with:
#!/usr/bin/python
which calls python2.7.x... If you have python3, then f2py may not find it. Do you start Python with the python3 command?
Note: It seems like f2py doesn't come for Python3 ('f2py3' or so).
Note: The error message you mention can come from a wrong shebang line at the start of f2py (the shebang is the first line of the f2py executable - which is just a Python script). Normally it is
#!/usr/bin/python
or maybe
#!/usr/local/bin/python
if you compiled Python yourself. If it's neither of these, start suspecting. Maybe f2py was installed by some confused tutorial or demo.

How to get program to not throw "Error: Can't load Floyd's algorithm library"

another question for all of you-
So i am trying to get a program called Pysomap to work (its basically ISOMAP but for python[http://web.vscht.cz/spiwokv/pysomap/]), i follow the directions best as i can, building it on Ubuntu, Windows, and Fedora (prebuilt libraries), but cant seem to get it to work. On windows (which is the preferred implementation platform), every time i go to python and import pysomap, it gives me the above error. Anybody know how to solve this?
Thanks
-J
In the pysomap directory, create a setup.py file with the following content:
from distutils.core import setup, Extension
setup(name="pysomap",
version="2007.07",
url ="http://web.vscht.cz/spiwokv/pysomap/",
description="A library for isometric feature mapping.",
license='Unknown',
author="Vojtech Spiwok",
py_modules=["floyd", "pysomap"],
ext_modules=[Extension('_floyd', ["floyd_wrap.c", "floyd.c"])]
)
Build the _floyd extension (assuming you have a Python distutils compatible C compiler installed):
python setup.py build_ext --inplace
Run demo.py:
python demo.py
Calculating distance matrix ............ 2.24 s
Using epsilon-isomap, epsilon = 0.500000, calculating 2-dimensional embedding
Calculating graph matrix ............... 1.21 s
Calculating the shortest path matrix ... 2.22 s
Multidimensionally scalling ............ 7.11 s
--------------------------------------------------
Total procedure ........................ 12.79 s
I had a look at the code because Isomap is a cool algorithm. The code doesn't look like it was written by someone familiar with Python, and the whole floyd extension module compilation is sort of shoddy - it actually didn't build for me, and I'm pretty sure that's your problem (they catch the import exception for the module and print out the error that you give).
I changed some hard-coded stuff in build_floyd.sh to different hard-coded values for my system. (A proper build system would make this unnecessary). Here's what worked in my Ubuntu system. I tested and it ran ok.
#!/bin/sh
# run this script to generate library for Floyd's
# algorithm library
echo "generating input files using SWIG ..."
swig -python floyd.i
echo "compiling ..."
# change compiler if you use other than gcc
gcc -c floyd.c floyd_wrap.c -I/usr/include/python2.6 -fPIC
echo "linking ..."
ld -shared floyd.o floyd_wrap.o -o _floyd.so
#echo "for SELinux you must run chcon ..."
#chcon -t textrel_shlib_t _floyd.so
You may also have to change the first line in demo.py to #!/usr/bin/env python.
This source code is over 5 years old and the build script for floyd looks to assume hard-coded python2.4.
It seems pretty clear that your floyd module did not build. You will most likely have to go back to the build step and ensure that you are properly generating a _floyd.so.
If you built it correctly, then this should not fail for you:
python -c "import _floyd"

Categories

Resources