[英]importing sibling file in a module in python3
我的python项目有这个目录结构
├── main.py
└── util
├── color.py
├── __init__.py
└── student.py
main.py
是:
from util.student import fun
fun("calling fun from main")
color.py
是:
def color_fun(a):
print(a)
student.py
是:
from color import color_fun
def fun(var):
color_fun(var)
if __name__ == "__main__":
fun("calling fun from student")
__init__.py
为空
当我尝试运行python3 student.py
它按预期工作。 但是当我尝试运行python3 main.py
它没有按预期工作,而它在python2中工作正常。
我想运行python3 student.py
以及python3 main.py
我怎么能实现这个?
您需要做的就是在student.py
稍作修改
def fun(var):
color_fun(var)
if __name__ == "__main__":
from color import color_fun
fun("calling fun from student")
else:
from util.color import color_fun
Python3的PYTHONPATH
导致了这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.