gpt4 book ai didi

python - 尝试从导入的脚本导入脚本时出现“ModuleNotFoundError”

转载 作者:行者123 更新时间:2023-12-01 07:46:53 27 4
gpt4 key购买 nike

我的文件夹结构:

ttsTacotron.py
Tacotron-2
|..
|tacotron|
|train.py
|synthetizer.py
|...
|synthesize.py # imports hparams.py
|hparams.py
...

当我直接调用 synthesize.py 时,它工作正常,所有导入都成功处理。当我在 ttsTacotron.py 中导入 synthesize 并调用它时,导入 synthesize 的模块失败。具体来说,它在导入 hparams 时失败。

ttsTacotron.py:

import fire
import sys
import os

import importlib
foobar = importlib.import_module("Tacotron-2.synthesize")

有问题的 Tacotron 文件夹是 this repository但问题不太可能是针对它的。

备注:我使用 importlib 来处理子文件夹中的 - 。由于各种原因无法真正重命名。

我的目标:能够调用synthetize的方法并能够从根文件夹中的脚本导入tacotron模块。

最佳答案

这是因为,当运行ttsTacotron.py时,Python会在包含ttsTacotron.py的目录(以及系统模块目录)中查找所有非相对导入的模块,与此处无关),但 hparams.py 位于 Tacotron-2 目录中。最简单的修复可能是将 Tacotron-2 添加到查找模块的目录列表中;这也消除了使用 importlib 的需要。

import sys
sys.path.insert(1, 'Tacotron-2')

import synthesize as foobar

关于python - 尝试从导入的脚本导入脚本时出现“ModuleNotFoundError”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56420007/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com