Open6

pip install -eでインストールしたパッケージはimportできない

豚&紙箱豚&紙箱

espnetのソースコードをローカルにダウンロードし、pip install -e .で仮想環境にインストールしました。しかし、以下のソースコードを実行すると、

from espnet2.bin.tts_inference import Text2Speech

ImportErrorが発生します。

Traceback (most recent call last):
  File "D:\espnet-lab\tts.py", line 1, in <module>
    from espnet2.bin.tts_inference import Text2Speech
  File "D:\espnet-lab\espnet-src\espnet2\__init__.py", line 3, in <module>
    from espnet import __version__  # NOQA
ImportError: cannot import name '__version__' from 'espnet' (unknown location)

espnet.__version__espnet-src/espnet/__init__.pyリンク)で設定されます。

"""Initialize espnet package."""

import os

dirname = os.path.dirname(__file__)
version_file = os.path.join(dirname, "version.txt")
with open(version_file, "r") as f:
    __version__ = f.read().strip()

つまり、__init__.pyは実行されななかったです。なぜでしょう。

豚&紙箱豚&紙箱

__init__.pyが実行するタイミングを調べてみます。

豚&紙箱豚&紙箱

'espnet' (unknown location)が気になります。__init__.pyが実行しなかったのは、espnetパッケージが見つからなかったのかもしれません。

豚&紙箱豚&紙箱

espnet2\__init__.pyに入っているソースコードは一行だけです。

  from espnet import __version__  # NOQA

#NOQAはlinter向けのコメントです。

Adding # noqa to a line indicates that the linter (a program that automatically checks code quality) should not check this line. Any warnings that code may have generated will be ignored.

That line may have something that "looks bad" to the linter, but the developer understands and intends it to be there for some reason.

https://stackoverflow.com/a/45346791

豚&紙箱豚&紙箱

espnet-labフォルダにespnetという子フォルダが存在します。それはモジュール検索に影響したかもしれません。その子フォルダを削除してもう一回tts.pyを実行したら、import文は成功しました。