How do I import a Python script from a sibling directory?

偶尔善良 提交于 2019-11-30 04:15:53

If all occurring directories are Python packages, i.e. they all contain __init__.py, then you can use

from ..bar_dir import bar

If the directories aren't Python packages, you can do this by messing around with sys.path, but you shouldn't.

You can use the sys and os modules for generalized imports. In foo.py start with the lines

import sys
import os
sys.path.append(os.path.abspath('../bar_dir'))
import bar
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!