[英]Python intra-package imports don't work at all
My directory structure is like the following: 我的目录结构如下:
toplevel/
subdir/
__init__.py
foo/
__init__.py
foo.py
bar/
__init__.pt
bar.py
if i try to from foo import foo
in bar.py, and execute bar.py from subdir
with python3 bar/bar.py
it says foo was not found. 如果我尝试
from foo import foo
bar.py中的from foo import foo
,并使用python3 bar/bar.py
从subdir
执行bar.py,则说明未找到foo。 I used os.getcwd()
and it is toplevel/subdir
, so it should work. 我使用了
os.getcwd()
,它是toplevel/subdir
,所以它应该可以工作。 I've tried relative imports, but none work. 我已经尝试了相对导入,但是没有用。
If you must do this, you can use sys.path
to see what paths interpreter can load module from. 如果必须执行此操作,则可以使用
sys.path
查看解释器可以从中加载模块的路径。
and then via sys.append('path you want add')
to add path 然后通过
sys.append('path you want add')
路径sys.append('path you want add')
添加路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.