Best way to import a python module into the root of a package hierarchy from another module in one package

I am writing a package P. There is a module m0 in the root of P. Somewhere inside P there are modules m1, m2, ... that need to be imported from m0

Of course, I can write in each of these modules:

from P.m0 import ...

However, if I change the name P, I need to review all the places and rewrite such statements.

I could also use relative imports, but if I translate the module at a different level in the package hierarchy, I have to fix the number of points.

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

+5
source share
2 answers

It's impossible.

, , , , .

, - .

+1

, .

; , - testsuite package/.../module.py,

THEN , , , import firstthing, /firstthing.py :

import sys
import os.path
packageDir = os.path.split(__name__)[0]
sys.path[:] = sys.path+[packageDir]  # or maybe you want it first...

, python, . , python ( ), , .


. , python. , . , "x", import x, , , . " os.walk", , ( - - ). python sys.path. , , - python .

, $PYTHONPATH, , , , .

0

All Articles