Howto import modules with dependencies in the same absolute/relative path with imp?

前端 未结 2 983
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-11 15:31

Is there a way to import modules with dependencies within the same directory using an absolute/relative path with \"imp\" ?

Here follows the directory structure:

2条回答
  •  無奈伤痛
    2021-01-11 16:23

    You need to have __init__.py in importFrom directory.

    In [5]: ls importFrom/
    dependance.py  dependant.pyc  dependence.pyc  __init__.pyc
    dependant.py   dependence.py  __init__.py
    
    In [6]: from importFrom import dependant
    
    In [7]: dependant.dependence
    Out[7]: 
    

    Instead of importing dependant in test, I have used Python interpreter to show the output. In dependant.py, dependance is imported.

提交回复
热议问题