简体   繁体   中英

Import parent directory package without sys.path.append

I have a project structured as this:

parent/
   sub1/
      __init__.py
      directoryManager.py
   sub2
      tst.py

in tst.py, I am trying to import directoryManager as ld from sub1, is there anyway to import it without using sys.path.append ?

Thanks very much

You could use:

from .. import directoryManager

The extra . goes one dictionary up

If this is going to be a package installed to path from parent parent.sub1 import directoryManager

import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from sub1 import directoryManager

This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM