python项目中不同文件夹py源文件之间如何相

如题所述

使用 from ...  import ... 可以导入其它文件夹中的模块,举个例子,在 main.py 模块中调用 在其它文件夹中的 test.py, test2.py 模块,test.py 在utils文件夹中, test2.py 在utils\sub 文件夹中,代码如下:

main.py

#coding=utf-8

# 使用 from ...import ... 导入test,test2模块
from utils import test
from utils.sub import test2

test.hello()
test2.hello()

test.py 文件,在 utils 文件夹下

#coding=utf-8

def hello():
    print('Hello, Greetings from', __name__)

test2.py 文件,在 utils\sub 文件夹下

#coding=utf-8

def hello():
    print('Hello, Greetings from', __name__)

运行结果:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-07-30
通过import ** 导入包文件
相似回答