【问题标题】:'ModuleNotFoundError' when trying to import script from imported script尝试从导入的脚本导入脚本时出现“ModuleNotFoundError”
【发布时间】:2019-06-03 08:47:30
【问题描述】:

我的文件夹结构:

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 模块。

【问题讨论】:

    标签: python python-importlib


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-03-21
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多