6
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

ImportError: attempted relative import with no known parent packageの解決方法

TensorFlow Neural Machine Translation (seq2seq) Tutorialを試してる時に下記エラーに遭遇

python36の場合
(py36) C:\tf\nmt> python -m nmt.nmt --xxxxxx
Traceback (most recent call last):
  File "C:\anaconda3\envs\tf\lib\runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "C:\anaconda3\envs\tf\lib\runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "C:\tf\nmt\nmt\nmt.py", line 28, in <module>
    from . import inference
ImportError: attempted relative import with no known parent package
python35の場合
(py35) C:\tf\nmt> python -m nmt.nmt --xxxxxx
Traceback (most recent call last):
  File "C:\anaconda3\envs\tf35\lib\runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "C:\anaconda3\envs\tf35\lib\runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "C:\tf\nmt\nmt\nmt.py", line 28, in <module>
    from . import inference
SystemError: Parent module '' not loaded, cannot perform relative import

原因

nmt.pyがあるディレクトリ(C:¥tf¥nmt)で下記コマンドを実行していたことが原因

tf
├── nmt(ココ)
│   ├── nmt.py
│   ├── inference.py
│   └── train.py
C:¥tf¥nmt> python -m nmt.nmt --xxxxx

解決方法

nmt.pyがあるディレクトリの一つ上(C:¥tf)に移動し、下記コマンドを実行する

tf(ココ)
├── nmt
│   ├── nmt.py
│   ├── inference.py
│   └── train.py
C:¥tf¥nmt> cd ..
C:¥tf> python -m nmt.nmt --xxxxx

オチ自体はとてもしょうもないのですが、割と長時間ハマった&Issueで困ってる人が結構いたので記事にしました。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
6
Help us understand the problem. What are the problem?