Search code examples
pythonimportfiona

".c" files called by fiona. How to import them?


I downloaded fiona today. when I try to import it in Python using 'import fiona', I get the following error:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import fiona
  File "C:\Python27\lib\site-packages\fiona\__init__.py", line 72, in <module>
    from fiona.collection import Collection, supported_drivers, vsi_path
  File "C:\Python27\lib\site-packages\fiona\collection.py", line 7, in <module>
    from fiona.ogrext import Iterator, ItemsIterator, KeysIterator
ImportError: No module named ogrext

I checked in my site-packages folder, and ogrext is a "C" file. I tried commenting out the import to see if it wasn't necessary, but this of course threw another error.

Specifically, how do I resolve this import error?

More generally, how does one resolve errors involving importing C files into a python library?


Solution

  • You can't just install any module by copying all the files to site-packages. Some modules are pure Python, but there are many with extensions written in other languages (C, C++, Fortran, etc.) that need to be compiled and linked into libraries before being used, and fiona is one of them. This compilation can be done at several stages - by the author, before distributing the module as a wheel, during the pip install process, or by downloading the package's source, unzipping/tarring it, and running python setup.py install. Unfortunately, Windows doesn't come with a compiler by default, so you either need to install and configure your system for gcc or Visual Studio, or use another method, such as a precompiled installer. Fortunately, fiona is available from Christoph Gohlke's Python Extension Packages for Windows Repository here. Download the installer for your version and bit-ness of Python, delete the fiona folder in site-packages, then run the installer. This site contains a large number of packages for scientific computing, and is my go-to resource when I need to install a new module, especially if it has extensions.

    EDIT

    Upon further inspection, it appears that fiona also requires the GDAL module, as well as six, both of which can be downloaded from Gohlke's repository. I first installed fiona only (I already had six installed), and got a missing DLL error. I then installed GDAL, and import fiona worked just fine - I'm not familiar with the module, so I didn't do any further testing, but hopefully everything should work now.