-2 votes

L'importation en Python entre trois fichiers ou plus ne fonctionne pas

J'ai les codes eg1.py , eg2.py , eg3.py eg3.py importe eg2.py qui à son tour importe eg1.py

Lorsque je lance eg3.py pour la première fois, tout va bien. Si je l'importe encore et encore, seul eg3.py s'exécute.

J'ai besoin d'une solution pour cela.

Je vais coder eg3.py de manière à ce que :

while(1):
    import eg2.py

Où je me suis trompé, merci de me donner une solution.

7voto

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.

Dans eg2.py :

def my_func():
    # do useful stuff
    pass

Dans eg3.py

import eg2
while True:
    eg2.my_func()

1voto

unwind Points 181987

Hein ? On ne peut pas boucler un import Ils sont mis en cache, ce qui ne fait rien d'autre que de gaspiller des cycles, après la première itération.

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

0voto

mavnn Points 3359

Si vous importez un module déjà importé, le code exécutable de ce module ne sera pas réexécuté.

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

La suppression du module dans le fichier sys.modules entraînera 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!
>>>

Éditer : Aussi, ce qu'a dit heikogerlach : il vaut mieux appeler des fonctions dans les modules déjà importés plutôt que de les effacer/recharger la plupart du temps.

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X