[Python3] Relative imports and 'grandparent' modules

On python 2 modules do:

from __future__ import absolute_import

Which will give you the syntax you want, python 3 imports.

Then first off, you should always put an __init__.py in those directory, instead of making it work has a directory with scripts you can treat it has an object.

Something like:

paper_figures/
    __init__.py
    figure1/
        __init__.py
        ....
    figure2/
        __init__.py
        .....
    figure3/
        __init__.py
        .....

Secondary modules in those can be used to split requirements for each files if you need to. So when you have your modules all you need to do is:

main.py (outside of paper figures):

import paper_figures
paper_figures.figureN.what_ever 

paper_figures/__init__.py:

from . import figure1
from . import figure2 
from . import figure3

figureN/__init__.py

from . import # ......

Those init scripts are extremely useful to treat those directories has objects. Now i don't think you should be splitting up your modules that much based on what you named them. If you have different stuff, its better to store all things that relate to each other in one place like:

paper_figures/
    __init__.py
    figures/
        __init__.py
        ....
        ....
    data/
        ... 

With a data folder somewhere if you need to split data you usually don't put it with the scripts directory, unless it important only to the current scope of the program. (which is rare)

Every script can be ran alone without being in different directories, i have no clue why you'd think of it this way. You can run functions on there own, why would you need to split them by entire modules to isolate them, you are either in the scope of a function or out of it. Going farther then needed only raises complexity.

/r/learnpython Thread