2009-04-01 6 views
-2

Je VHA codes eg1.py, eg2.py, eg3.py importations eg3.py eg2.py qui des importations tour eg1.pyImportation en Python entre trois ou plusieurs fichiers ne fonctionnent pas

Quand je lance EG3 .py pour la première fois tout va bien Si je l'importe encore et encore seulement eg3.py exécute

J'ai besoin d'une solution pour cela.

Je coderez eg3.py de telle sorte que:

while(1): 
    import eg2.py 

Où je suis allé wrong.Please me donner une solution.

+0

Cela n'a aucun sens. "run eg3.py" et "import again" ne sont PAS la même chose. Voulez-vous dire "exécuter eg3.py à nouveau?" Ou êtes-vous en cours d'exécution à partir de IDLE, puis en l'important après l'exécution? Essayez-vous de lancer en important? À quoi ressemble le code? –

Répondre

7

Voulez-vous exécuter le code dans eg2.py lorsque vous l'importez? Ce n'est pas une bonne solution. Vous devriez avoir une fonction contenant votre code dans eg2.py et ensuite exécuter cette fonction dans votre boucle while.

En eg2.py:

def my_func(): 
    # do useful stuff 
    pass 

Dans eg3.py

import eg2 
while True: 
    eg2.my_func() 
1

Huh? Vous ne pouvez pas boucler un import, ils sont mis en cache afin qu'il ne fasse vraiment rien sauf les cycles de déchets, après la première itération.

Comment savez-vous que "seulement eg3.py" s'exécute?

+0

je vous remercie .... je veux savoir en profondeur à propos de l'importation. Quel document devrais-je me référer? – user46646

0

Si vous importez un module déjà importé, le code exécutable dans ce module ne sera pas relancée.

Par exemple:

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

Suppression du module de sys.modules forcera un rechargement complet:

Par exemple:

>>> 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: En outre, ce heikogerlach dit: vous » Il vaut mieux appeler des fonctions dans les modules déjà importés que de les supprimer/les recharger la plupart du temps.

Questions connexes