Making python import statements 'insensitive' to working dir of launching script
Question: I am new to python and am running into an unusual situation with my imports.
Basically, I am just trying to organize things into packages. As an example,
see this basic directory structure:

    
    
       root
        |
     -------
     |     |
     a     b
    

If I 'launch' using a script in root and use scripts in 'a' that import
scripts from 'b', then it seems like my imports in the scripts in 'a' need to
be relative to the location of the 'launch' script (in root). This is OK, but
I am using IntelliJ as my IDE and when I am looking at a script in the 'a'
directory, IntelliJ thinks the imports are bad because they are not 'relative'
to the 'a' directory.

Does this make sense? Am I doing something wrong?

Thanks

_**__**_Edit Follows_** ___ _**

I have imports like these (my 'b' has children):

    
    
     from b.bchild import SomeClass
     from b import anotherBChild as blah
    

I tried adding 'b' as a source root in IntelliJ, but 'b' is a child of the
main source root so IntelliJ complains that source directories should not
intersect.

If I change my imports to be root.b and so on, then I get runtime failures
when the script from root is run.

Each of my directories has an empty **init**.py in it.

I should probably re-iterate that everything runs just fine... I just can't
seem to get IntelliJ to understand the import statements which made me think I
was doing something wrong.


Answer: If I understand your question correctly I believe this solution should work:

You need to add the parent of your "root" directory to pythonpath.

Then you place an empty

> `__init__.py`

file in "root" and every child of "root" and all their children and so on and
so forth.

When this is done you can import scripts quite easily.

Example:

Let's say that directory **"a"** contains the script **foo.py** and directory
**"b"** contains the script **bar.py**.

Now you want to import **bar** in **foo** , simply add this line to **foo** :

import root.b.bar as bar

EDIT: I have no experience using intellij with python. But if it is anything
like PyCharm (both made by jetbrains) then you also need to add the parent of
your "root" folder to intellij as you would add a project

(like wim said in his comment).

