4

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

Jialiang Zhou
  • 83
  • 1
  • 6

2 Answers2

1
import os
import sys

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

from sub1 import directoryManager

This should work.

chemical
  • 380
  • 1
  • 10
0

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

Xantium
  • 10,258
  • 9
  • 56
  • 83
  • it gives me attempted relative import in non package error – Jialiang Zhou Feb 13 '18 at 23:50
  • @JialiangZhou Does this post help: https://stackoverflow.com/questions/14664313/attempted-relative-import-in-non-package-although-packages-with-init-py-in – Xantium Feb 13 '18 at 23:58
  • well,not really cause the tst.py are scheduled to run via a server using python not python -m, I was thinking if I can put the directoryManager to a root directory of the computer – Jialiang Zhou Feb 14 '18 at 00:02
  • @JialiangZhou Unless I'm understanding the meaning of `root dictionary` incorrectly `from parent.sub1 import directoryManager` sould work – Xantium Feb 14 '18 at 00:11
  • I add the package to python/site-packages but it works in Linux only . In windows, I put is at python27/Lib/site-packages but doesn't work – Jialiang Zhou Feb 14 '18 at 00:13
  • @JialiangZhou Hmm is site-packages added to the path variable? – Xantium Feb 14 '18 at 00:16
  • I think so cause I saw other scrip import package located in site-packages – Jialiang Zhou Feb 14 '18 at 00:23
  • @JialiangZhou All I can suggest is create your setup.py file and install it. – Xantium Feb 14 '18 at 00:25