2009-04-01 4 views
-2

を動作していない3つの以上のファイル間でPythonでインポートeg2.py、私はEG3を実行すると、今度は輸入がは、私はコードがeg1.py肝炎

をeg1.py eg2.py eg3.py eg3.py輸入.py初めての場合はすべてうまくいく もし私が何度もそれをインポートした場合、eg3.pyが実行されます

私はこれを解決する必要があります。私が行った

while(1): 
    import eg2.py 

がwrong.Pleaseが私に解決策を与える:

私があるようにeg3.pyコードになります。

+0

これは意味をなさない。 "run eg3.py"と "import again"は同じものではありません。 「eg3.pyをもう一度実行する」という意味ですか?または、IDLEから実行して、実行した後にインポートしていますか?インポートして実行しようとしていますか?あなたは何のようにコード化していますか? –

答えて

7

あなたはそれをインポートするときにeg2.pyでコードを実行しますか?それは良い解決策ではありません。 eg2.pyにあなたのコードを含む関数を持たせておき、whileループでこの関数を実行する必要があります。 eg2.pyで

:eg3.py

import eg2 
while True: 
    eg2.my_func() 
1

Huh? importをループすることはできません。それらはキャッシュされているため、最初の反復後には無駄なサイクルを除いて何もしません。

"only eg3.py"が実行されていることをどのように知っていますか?

+0

ありがとうございました....私はインポートについて深く知りたいです。どのドキュメントを参照するべきですか? – user46646

0

def my_func(): 
    # do useful stuff 
    pass 

あなたは既にインポートされたモジュールをインポートする場合、そのモジュール内の実行可能コードは再実行されません。

例:

>>> import this 
The Zen of Python, by Tim Peters 

Beautiful is better than ugly. 
Explicit is better than implicit. 
Simple is better than complex. 
Complex is better than complicated. 
Flat is better than nested. 
Sparse is better than dense. 
Readability counts. 
Special cases aren't special enough to break the rules. 
Although practicality beats purity. 
Errors should never pass silently. 
Unless explicitly silenced. 
In the face of ambiguity, refuse the temptation to guess. 
There should be one-- and preferably only one --obvious way to do it. 
Although that way may not be obvious at first unless you're Dutch. 
Now is better than never. 
Although never is often better than *right* now. 
If the implementation is hard to explain, it's a bad idea. 
If the implementation is easy to explain, it may be a good idea. 
Namespaces are one honking great idea -- let's do more of those! 
>>> import this 
>>> 

sys.modulesからモジュールを削除すると、完全なリロードを強制します:

例:

>>> import sys 
>>> import this 
The Zen of Python, by Tim Peters 

Beautiful is better than ugly. 
Explicit is better than implicit. 
Simple is better than complex. 
Complex is better than complicated. 
Flat is better than nested. 
Sparse is better than dense. 
Readability counts. 
Special cases aren't special enough to break the rules. 
Although practicality beats purity. 
Errors should never pass silently. 
Unless explicitly silenced. 
In the face of ambiguity, refuse the temptation to guess. 
There should be one-- and preferably only one --obvious way to do it. 
Although that way may not be obvious at first unless you're Dutch. 
Now is better than never. 
Although never is often better than *right* now. 
If the implementation is hard to explain, it's a bad idea. 
If the implementation is easy to explain, it may be a good idea. 
Namespaces are one honking great idea -- let's do more of those! 
>>> del(sys.modules["this"]) 
>>> import this 
The Zen of Python, by Tim Peters 

Beautiful is better than ugly. 
Explicit is better than implicit. 
Simple is better than complex. 
Complex is better than complicated. 
Flat is better than nested. 
Sparse is better than dense. 
Readability counts. 
Special cases aren't special enough to break the rules. 
Although practicality beats purity. 
Errors should never pass silently. 
Unless explicitly silenced. 
In the face of ambiguity, refuse the temptation to guess. 
There should be one-- and preferably only one --obvious way to do it. 
Although that way may not be obvious at first unless you're Dutch. 
Now is better than never. 
Although never is often better than *right* now. 
If the implementation is hard to explain, it's a bad idea. 
If the implementation is easy to explain, it may be a good idea. 
Namespaces are one honking great idea -- let's do more of those! 
>>> 

編集:また 、言っheikogerlach:あなたは」すでにインポートされたモジュールの呼び出し関数は、ほとんどの場合それらを削除/再ロードするよりもうまく機能します。

関連する問題