2009-04-01 12 views
-2

I hav Codes eg1.py, eg2.py, eg3.py eg3.py Einfuhren eg2.py was wiederum die Einfuhrenin Python importieren zwischen drei oder mehr Dateien nicht funktioniert

eg1.py Wenn ich eg3 laufen .py zum ersten Mal ist alles in Ordnung Wenn ich es wieder und wieder importieren nur eg3.py läuft

Ich brauche eine Lösung dafür.

werde ich eg3.py so codieren, dass:

while(1): 
    import eg2.py 

Wo ich wrong.Please mir eine Lösung geben ging.

+0

Das ergibt keinen Sinn. "run eg3.py" und "import again" sind NICHT das Gleiche. Meinst du "run eg3.py nochmal?" Oder laufen Sie von IDLE und importieren dann nach dem Ausführen? Versuchen Sie, durch Importieren zu laufen? Wie sieht dein Code aus? –

Antwort

7

Möchten Sie den Code in eg2.py ausführen, wenn Sie ihn importieren? Das ist keine gute Lösung. Sie sollten eine Funktion haben, die Ihren Code in eg2.py enthält und dann diese Funktion in Ihrer while-Schleife ausführen.

In eg2.py:

def my_func(): 
    # do useful stuff 
    pass 

In eg3.py

import eg2 
while True: 
    eg2.my_func() 
1

Huh? Sie können keine import Schleife, sie sind zwischengespeichert, so dass es wirklich nichts außer Abfallzyklen nach der ersten Iteration tut.

Woher wissen Sie, dass "nur eg3.py" läuft?

+0

danke .... ich möchte eingehend über den Import wissen.Welches Dokument sollte ich beziehen? – user46646

0

Wenn Sie ein Modul importieren, die bereits importiert wird, ausführbarer Code in diesem Modul nicht erneut ausgeführt werden.

ZB:

>>> 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 
>>> 

das Modul von sys.modules löschen, wird ein komplettes Neuladen erzwingen:

ZB:

>>> 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! 
>>> 

Edit: Auch, was heikogerlach sagte: Sie‘ Es ist besser, Funktionen in den bereits importierten Modulen aufzurufen, als sie die meiste Zeit zu löschen/neu zu laden.

Verwandte Themen