4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-10-02

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で困ってる人が結構いたので記事にしました。

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?