导入由另一个模块导入的模块

时间:2021-01-14 18:47:17

标签: python module pycharm

我有 3 个 python 脚本。 File1.py、File2.py 和 helpers.py helpers 导入到 File2 中,当我运行它时,它可以工作。 当我在文件 1 中导入文件 2 时,当我从文件 2 调用文件 2 时,我收到错误消息:文件 1 中没有模块助手!! 请注意 file2 和 helpers 具有相同的目录,但 file1 没有! 另请注意,包含 file1 的文件夹包含一个文件夹“Time”,其中包含 file2 和 helpers 这是我的代码:

import sys
sys.path.append("..")
from Time import file2 as tc

def run_script():
    tc.main_log() #main_log() is a function defined in file2!
run_script()

当我导入时,我检测到文件(file2 和 helpers),但是当我运行脚本时,我收到以下错误: ModuleNotFoundError: 没有名为“helpers”的模块 如果我从文件 1 运行,file2 无法导入 helper,但如果我运行 file2,它可以从文件 2 正确导入。

0 个答案:

没有答案