import error while using python and Django
Question: I am making a website using the django api. Problem is I am getting a weird
import error. I have a function in a file which calls another function in
another file which in turn calls back a third function in the first file.

Problem is during that third function. When I try to import it I get an error

    
    
    cannot import deletefromS3.
    

A full stack trace is given below

<http://dpaste.com/1288190/>

Here are the snippets of the two modules:

topichandler.py:

    
    
    from sdbhandler.mediahandler import deleteMediaParent
    def deletefromS3(itemid,folder):
    itemid=folder+itemid
    bucket = connect_s3()
    for key in bucket:
        fname=key.split(".")[0]
        if(fname==itemid):
            bucket.delete_key(key)
            return []
    
    
    def deleteTopic(itemid,parentId='NULL'):
        sdb=connect()
        domain= sdb.get_domain(DOMAIN)
        rootitem = domain.get_item(itemid)
        if(parentId=='NULL'):
              query= 'select * from ' + DOMAIN + ' where itemName()="'+itemid+'"'
              rs = domain.select(query)
        else:
              rs = [rootitem]
        for  item in rs:
              deleteMediaParent(item.name)
              deletefromS3(item.name,'topicsK2/')
              domain.delete_attributes(item.name)
        deleteMediaParent(rootitem.name)
        deletefromS3(rootitem.name,'topicsK2/')
        domain.delete_attributes(rootitem.name)
    

mediahandler.py:

    
    
    from sdbhandler.topichandler import deletefromS3
    def deleteMediaParent(parentid):
    sdb=connect()
    domain = sdb.get_domain(DOMAIN)
    query = 'select * from '+ DOMAIN + 'where ' +FIELD_TopicID + ' = "' + parentid + '"'
    rs = domain.select(query)
    for item in rs:
        deleteQuestionParent(item.name)
        deletefromS3(item.name,'mediaK2/')
        domain.delete_attributes(item.name)
    

There are more dependencies but I cannot post my whole code that would be way
too much. Can I not import from the file from which a method was called?


Answer: The import is impossible because a module has to finish loading before things
can be imported from it. When the `topichandler` module loads, it tries to
import `deleteMediaParent` from `mediahandler`. But `mediahandler` tries to
import `deletefromS3` from `topichandler`, which triggers another attempt to
load `topichandler`. Python catches the infinite loop that’s about to happen
and raises an error instead.

Let’s look at this with a simpler example. Here is `foo.py`:

    
    
    #!/usr/bin/env python2.7
    
    x = 3
    
    from bar import y
    
    print x, y
    

and here is `bar.py`:

    
    
    from foo import x
    
    y = x
    

This gives the same error you got, for the same reason.

Although the best solution is probably to restructure your code into more
coherent standalone modules, there is a workaround. You can delay the import
by moving it into a function, as in:

    
    
    #!/usr/bin/env python2.7
    
    x = 3
    
    from bar import y
    
    print x, y()
    

`bar.py`:

    
    
    def y():
        from foo import x
        return x
    

Note that if you run this the `print` will actually be imported twice… again,
you probably are better off moving related functions into the same module.

