Python cannot import name if placed in module
Question: > **Possible Duplicate:**  
>  [How to do relative imports in
> Python?](http://stackoverflow.com/questions/72852/how-to-do-relative-
> imports-in-python)

I'm experiencing something that seems very random to me.

I have a folder structure much like this:

    
    
    dir A
        __init__.py is empty
        a.py imports stuff and b.py
    dir B
        __init__.py is empty
        b.py imports NOTHING
    

a.py raises an error (cannot import name b). This only happens while b is part
of module B. If I move it outside the directory, the import error does NOT
occur.

Any help would be appreciated. I must be overlooking something.


Answer: Did you try the [relative
import](http://docs.python.org/2/tutorial/modules.html#intra-package-
references)

    
    
    from ..B import b
    

?

* * *

EDIT: This does not apply if it doesn't matter where package `B` lives.

But you don't describe what exactly you do. As you may know or not, there are
several import forms:

    
    
    import module
    import package # imports package.__init__ under the name package
    import package.module
    from package import module
    import package
    from module import component
    from package.module import component
    

As you only write

> a.py imports stuff and b.py

I don't know what exactly happens: if you try to

    
    
    import b
    

that fails because `b` lives in the package `B`. So you need one of

    
    
    from B import b
    import B.b
    

Your comment above mentions a name clash. Which of two equally named packages
and modules have priority depends on in which directory you are: `'.'` is
normally at the very start of `sys.path`, so if you are directly under your
`utils` directory you might have a different experience than otherwise.

