如何从兄弟目录导入Python脚本? [英] How do I import a Python script from a sibling directory?
本文介绍了如何从兄弟目录导入Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是目录结构:
parent_dir/
foo_dir/
foo.py
bar_dir/
bar.py
如何将bar.py导入foo。 py?
How do I import bar.py into foo.py?
推荐答案
如果所有发生的目录都是Python 包,即它们都包含 __ init __。py
,然后你可以从..bar_dir导入栏中使用
If all occurring directories are Python packages, i.e. they all contain __init__.py
, then you can use
from ..bar_dir import bar
如果目录不是Python包,你可以通过弄乱 sys来做到这一点.path
,但你不应该。
If the directories aren't Python packages, you can do this by messing around with sys.path
, but you shouldn't.
这篇关于如何从兄弟目录导入Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文