Search code examples
pythonpython-2.7relative-import

Python relative import of an importable module not working


I need to use the function MyFormatIO which is a part of the neo library. I can successfully import neo and neo.io BUT I cannot use the MyFormatIO function. import neo.io doesn't spit out any errors but from neo.io import MyFormatIO returns NameError: name 'MyFormatIO' is not defined. How can this be if MyFormatIO is a part of neo.io? I am running python2.7 on CentOS.


Solution

  • MyFormatIO is not a class in neo.io.

    http://pythonhosted.org/neo/io.html#module-neo.io

    One format = one class

    The basic syntax is as follows. If you want to load a file format that is implemented in a generic MyFormatIO class:

    from neo.io import MyFormatIO reader = MyFormatIO(filename = "myfile.dat")

    you can replace MyFormatIO by any implemented class, see List of implemented formats

    You have to replace 'MyFormatIO' with a class from this list: http://pythonhosted.org/neo/io.html#list-of-io

    A quick way to check this kind of thing in the interpreter is with dir.

    import neo.io
    dir(neo.io)
    

    Those are the items that you can import or use from neo.io