System Paths and Modules
Question: I have the following setup:

    
    
    /project/
        /api/
            __init__.py
            test.py
        /modules/
            __init__.py
            api.py
    

I am trying to, from the /project/ directory, run api.py: `python
modules/api.py`

The api module attempts to import the test module from the api package, but
fails. I have tried the following:

    
    
    import api.test
    import project.api.test  # (with an __init__.py in my /project/ directory)
    

I have even attempted to add the api package's parent directory to the system
path as described:

    
    
    import sys
    import os
    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    

### Question

How can I set up my project in such a way that each package has knowledge of
other packages in it's parent directory, which is also the project's root
directory?


Answer: Your problem is that you have a `project/modules/api.py` **file** as well as a
`projects/api` **directory** with an `__init__.py` file in it - you're getting
descriptor collisions. Rename one of them to something else, and your code
should work:

    
    
    /project/
        /api/
            __init__.py
            test.py
        /modules/
            __init__.py
            foo.py
    

Then run `python modules/foo.py` and in foo.py:

    
    
    from ..api import test
    

or alternatively:

    
    
    import sys
    import os
    sys.path.append(os.path.abspath('../api'))
    import test
    

