Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to import python module at the root of a package hierarchy from another module in the same package

I write a package P. At the root of P there is a module m0. Somewhere inside P there are modules m1,m2,... that need to import from m0

Of course I can write in each of these modules:

from P.m0 import ...

However if I change the name of P I have to revisit all the places and rewrite such statements.

I could also use relative imports but if I move a module at a different level in the package hierarchy I have to go fix the number of dots.

There are some other reasons too, but bottom line, I really want to say import from the module m0 that is located at the root of my package, what is the best way to express this?

like image 567
gae123 Avatar asked Nov 04 '22 01:11

gae123


1 Answers

That's not possible.

However, if you perform major refactoring where you move around modules between subpackages having to update some relative imports is not a huge problem.

Same applies for renaming the top-level package name if you do not use relative imports - that could even be done really fast with search-and-replace over all your files.

like image 149
ThiefMaster Avatar answered Nov 11 '22 17:11

ThiefMaster