Importing fortran files to python - python

This is a very newbie question, but after searching in Google for a while, I haven't been able to find a solution. I'm writing a Python code using Eclipse (in Linux Mint) which needs some routines written in Fortran 77 (the code is in a file named fortran_code.f). I've used f2py to get the file fortran_code.so. This file is in the same folder as the Python code (../workspace/python_project/src). The Python code only includes:
import fortran_code
a = 10
fortran_code.fortran_subroutine(a)
Once an again, the result is:
Traceback (most recent call last): File
"/home/user/Documents/workspace/python_project/src/Main.py", line 7,
in <module>
import fortran_code ImportError: /home/user/Documents/workspace/python_project/src/fortran_code.so:
undefined symbol: PyCObject_Type
The Fortran subroutine code is:
SUBROUTINE fortran_subroutine(a)
REAL*8, intent(in) :: a
REAL*8 b
b=a*2
end subroutine fortran_subroutine
To get the .so file (fortran_code.so) I use:
f2py -c fortran_code.f -m fortran_code
The version of f2py is 2. And the version of Python is 2.7.4
Any help to solve this problem would be very useful.
Thanks!

In order to make it work I had to modify the Python Interpreter from Python 3.2 to Python 2.7 (since f2py produces code for Python 2.7). After editing a new Python module everything worked fine!!!
Thanks for all suggestions and comments, which were really helpful.

Related

gdb.printing not available in python2

First of all, let me start out by saying, I am using gdb version 7.7 built --with-python
the python version is 2.6.x
And No I cannot upgrade them, since I'm limited by the devtools specifically meant for debugging my companies code.
There a lot of useful gdb-python scripts but all of them require me to
import gdb.printing
But I get an import error
(gdb)python import gdb.printing
Traceback (most recent call last):
File "<string>", line 1, in ?
ImportError: No module named printing
Error while executing Python code.
Is there any way to get the gdb.printing module in this version of gdb and python? (python3 works fine).
I would really appreciate any inputs for this.

Is it possible to make my Windows Python installation aware of extra libraries it can use when running inside a Cygwin terminal?

I would like to use the following Python package, which requires ncurses: https://pypi.python.org/pypi/blessings
I have the Windows version of Anaconda Python installed, and designated as my Python distribution.
I have also have a Cygwin installation. When I run python -i within the provided Cygwin terminal, Anaconda Python starts up -- great!
If I then try import blessings, I'll get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "A:\anaconda\lib\site-packages\blessings\__init__.py", line 5, in <module>
import curses
File "A:\anaconda\lib\curses\__init__.py", line 15, in <module>
from _curses import *
ImportError: No module named _curses
Alright, so what's happening is that Anaconda Python is understandably not picking up Cygwin's curses.
Now, does it even make sense to try and somehow make Anaconda aware of ncurses, only within the context of usage in a Cygwin terminal? I suspect it isn't, and that I am missing something conceptually?
You won't be able to use Cygwin Python's curses module with CPython unfortunately. Modules for the two Pythons are incompatible for the following reasons (and more):
Windows Python directly calls the Win32 API (via either the Visual Studio 2008, 2010 or 2015 C-Runtimes) while Cygwin Python links to the Cygwin POSIX API which is layered on top of the Visual Studio 6.0 C-Runtime. Mixing different C-Runtimes in a single process is a very bad idea: http://siomsystems.com/mixing-visual-studio-versions/, never mind further abstracted through a POSIX API.
Regardless of the C-Runtime differences, Cygwin implements the LP64 model while Windows implements the LLP64 model which would make the 64-bit size of long different between each, so some structures could be differently sized. They would likely be anyway due to #ifdefs in the code.
Is there any reason you can't use colorama?
The documentation for blessings suggests that should work.

Beginner, Error in python regarding site-packages/celltool/numerics/fitpack/_fitpack.so

I'm doing some image analysis for my research, and it involves using CellTool (http://pantheon.yale.edu/~zp2/Celltool/), Python, and numpy. I'm on a OS X 10.8.3. On my old laptop that crashed, I was able to run my commands fine, but I got a new one and things are not going as smoothly!
I believe I have CellTool and numpy installed correctly, and I'm just using Python 2.7.2 that's standard on Mac. But when I try to run this python script "calculate_distances.py" command, I get this error:
Yuxins-MacBook-Pro:Modified_Contours yuxinsun$ python calculate_distances.py
Traceback (most recent call last):
File "calculate_distances.py", line 24, in <module>
normals = contours[n-1].inward_normals()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celltool/contour/contour_class.py", line 384, in inward_normals
import celltool.numerics.fitpack as fitpack
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celltool/numerics/fitpack/__init__.py", line 1, in <module>
from fitpack import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celltool/numerics/fitpack/fitpack.py", line 34, in <module>
import _fitpack
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celltool/numerics/fitpack/_fitpack.so, 2): Library not loaded: /usr/local/lib/libgfortran.2.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celltool/numerics/fitpack/_fitpack.so
Reason: image not found
And I have no idea what any of it means...I only need to run this python script and get the file that it spits out.
Am I just missing the "fitpack" whatever that is? If so, how do I install it? Or what can I do to fix this problem.
You need to make sure you have those installed per installation requirements:
If one of the above binary installers did not suit your needs,
please install Celltool from the source code.
This depends on only three things:
1.Python (version 2.3 or later).
2. NumPy 1.0.4 or later.
3. Working C/C++ and Fortran compilers.
and it looks like your Fortran compiler libs are not installed.
This might be helpful: https://scicomp.stackexchange.com/questions/2469/how-should-i-install-a-fortran-compiler-on-a-mac-os-x-10-x-x-4

Is Jython 2.5.1 incompatible with Python 2.7?

I'm trying to do some pretty simple stuff in Jython within Java. My Python path, and by exension, my Jython path, is set to the following:
$ python -c "import sys ; ':'.join(sys.path)"
:/usr/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/usr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0:/usr/lib/pymodules/python2.7:/usr/lib/python2.7/dist-packages/ubuntu-sso-client:/usr/lib/python2.7/dist-packages/ubuntuone-client:/usr/lib/python2.7/dist-packages/ubuntuone-control-panel:/usr/lib/python2.7/dist-packages/ubuntuone-couch:/usr/lib/python2.7/dist-packages/ubuntuone-installer:/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol:/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode
(An easier to read list can be found here.)
I've set my Jython path by defining -Dpython.path=":/usr/lib/python2.7:...", and it's working fairly well.
I'm experiencing issues, however, whenever I try to do anything significant, like import the os module:
>>> import os
Exception in thread "main" Traceback (most recent call last):
File "<iostream>", line 2, in <module>
SyntaxError: ('no viable alternative at input \'""\'', ('/usr/lib/python2.7/os.py', 754, 18, ' bs = b""\n'))
What's going wrong here? Is Jython incompatible with Python 2.7? What can I do to get things working?
You cannot mix Python versions and implementations. The Cpython you are using is 2.7 and the Jython is 2.5 so they canot have the same path even if you did this for Cpython 2.5 and 2.7
the actual error you are seeing is because jython loads the python C libraries and gets to code that calls C code which jython does not have.
You can share pure python code that is for the same version e.g. python 2.5 and jython 2.5 but unlikely to manage any other mix.

How to Make a PyMe (Python library) Run in Python 2.4 on Windows?

I want to run this library on Python 2.4 in Windows XP.
I installed the pygpgme-0.8.1.win32.exe file but got this:
>>> from pyme import core
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Python24\Lib\site-packages\pyme\core.py", line 22, in ?
import pygpgme
File "C:\Python24\Lib\site-packages\pyme\pygpgme.py", line 7, in ?
import _pygpgme
ImportError: DLL load failed: The specified module could not be found.
And then this pop up comes up
---------------------------
python.exe - Unable To Locate Component
---------------------------
This application has failed to start because python25.dll was not found. Re-installing the application may fix this problem.
---------------------------
OK
Do I need to "compile" it for Python 2.4? How do I do that?
While the pygpgme project does not clearly document it, it's clear from the error message you got that their .win32.exe was indeed compiled for Python 2.5.
To compile their code for Python 2.4 (assuming they support that release!), download their sources, unpack them, open a command window, cd to the directory you unpacked their sources in, and run python setup.py install. This will probably not work unless you have the right Microsoft C compiler installed (MSVC 6.0 if I recall correctly).
It's no doubt going to be much less trouble to download, install and use Python 2.5 for Windows (it can perfectly well coexist with your current 2.4, no need to remove that). Is that a problem?

Categories