python的import用法

1,主程序与模块在同一路径下

python的import用法

在程序testGetCRStatement.py中导入模块custom, 则直接使用import custom或from custom import *;

2, 程序所在目录是模块所在目录的父(或祖辈)目录


python的import用法

python的import用法


程序runAll.py中导入评级目录common下的log模块,需要在common下创建一个空文件__init__.py,然后使用 import common.log as log

3,主程序导入上层目录中或其他目录(平级)下模块

python的import用法

python的import用法


python的import用法

若在程序log.py中导入模块readConfig.py。首先需要在config下建立__init__.py文件,src下不必建立该文件。然后调用方式如下:

import sys

sys.path.append("..")

import config.readConfig as readConfig