0

I want to read Fortran 90 binary files in Python and hence there is this module which has been developed for that in Scipy called as "fortranFile", as given here fortranFile module

How do I include this module in python so that I can call it from my code like

import fortranFile

f = fortranFile('file.bin')
x = f.readReals('f')

print x

right now, I get the error

   Traceback (most recent call last):
  File "readfortbin.py", line 5, in <module>
    from fortranFile import fortranFile 
ImportError: cannot import name fortranFile

I made a folder "fortranFile" in my /usr/lib/python2.7/dist-packages directory and added the "fortranFile.py" file to that folder. I know that I am missing something... Please suggest how I should proceed.

2
  • I think your problem is that it should be from fortranFile import FortranFile. I've added an answer below showing how to install the package from the PyPi package repository which has the added convenience of automatic updates when new versions are released. Commented Jun 26, 2013 at 2:54
  • Thanks, and sorry to bother again. I can import the module now, but It says "AttributeError: 'FortranFile' object has no attribute 'readReals'" though thats how the example code is implemented.. Any ideas? Commented Jun 26, 2013 at 3:00

1 Answer 1

2

To quickly address your question, I think your import statement is wrong. Specifically I think from fortanFile import fortranFile should be from fortranFile import FortranFile. It is convention in Python is to capitalize every word in a class name.

On a different note there is a more up-to-date version of the module you're trying to install here and in the Python Package Index. You can easily install it using the Python package installer pip.

Important note: fortranfile depends on the popular numpy numerical analysis package.

On my Ubuntu 13.04 system I had to perform the following steps:

apt-get install -y libpython-dev # For compiling the numpy C wrappers.
                                 # May alternatively be called python-dev or python-devel.
pip install numpy
pip install fortranfile
Sign up to request clarification or add additional context in comments.

3 Comments

+1. I managed to install the fortranfile package, however, I am still getting the same error while using it. Thanks...
@arvind I think your import statement is wrong. If you installed the fortranfile package from PyPi using pip you should use the import statement from fortranfile import FortranFile. The second part of the import statement (a class name) has both words capitalized by convention.
Ah, I got it. Thanks for the help. The problem was in uppercase "fortranFile"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.