[Python-ideas] Relative Imports

Steven D'Aprano steve at pearwood.info
Fri Nov 9 19:10:43 EST 2018


On Fri, Nov 09, 2018 at 03:51:46PM -0800, danish bluecheese wrote:
> └── src
>     ├── __init__.py
>     ├── main.py
>     └── test
>         ├── __init__.py
>         └── test_main.py
> 
> assume the structure above. To be able to use relative imports with such
> fundamental structure either i can go for sys.path hacks or could run as a
> module from one further level parent.

I don't understand. From the top level of the package, running inside 
either __init__ or main, you should be able to say:

from . import test
from .test import test_main

>From the test subpackage, you should be able to say:

from .. import main

to get the src/main module, or 

from . import test_main

to get the test/test_main module from the test/__init__ module.

(Disclaimer: I have not actually run the above code to check that it 
works, beyond testing that its not a SyntaxError.)

What *precisely* is the problem you are trying to solve, and your 
proposed solution?



-- 
Steve


More information about the Python-ideas mailing list