Help us understand the problem. What is going on with this article?

ImportError: cannot import nameを解消する

More than 5 years have passed since last update.

コピペ

http://d.hatena.ne.jp/machua/20101026/1288099599

事象

以下のようなモジュール構造で、互いにモジュールを呼び合う場合に発生。

ファイル構成
view\
    __init__.py
    form1.py
    form2.py
form1.py
from veiw.fom2 import frm2
class frm1 ():
    ....
form2.py
from veiw.fom1 import frm1
class frm2 ():
    ....

原因

インポート時に、自分自身をインポートしているためにエラーとなっている(と思う)
ちなみに、組み込みモジュールと同じ名前のモジュールを作成した時も発生する。

解決策

グローバルな箇所でインポートをするのではなく、クラス内でインポートする。
例えば、コンストラクタ内や画面を呼び出す直前のファンクション内など。

form1.py
class frm1 ():
    def __init__(self):
        from veiw.fom2 import frm2
        self.fm = frm2()
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした