Another question about modules importing each other I know :(
Basically, I have a file main.py containing this :
class Main :
@staticmethod
def newMouton(mouton, i, j) :
listMoutons.append(mouton)
listMoutons.append(canvas.create_image(i * coteCarre, j * coteCarre + yMargin, img = imageMouton))
@staticmethod
def delMouton(mouton) :
moutonIndex = listMoutons.index(mouton)
canvas.delete(listMoutons[moutonIndex + 1]) #en ajoutant 1, on retrouve l'obj canvas image à supprimer
listMoutons.remove(mouton)
del listMoutons[moutonIndex:(moutonIndex + 2)]
from random import randint
from simulation import Simulation
#doing some other stuff such as creating the canvas variable and creating a Simulation instance
And I have another file simulation.py :
from main import Main
class Simulation:
def __init__(self) : self._moutons = []
def moutonMort(self, mouton) :
self._moutons.remove(mouton)
pos = mouton.getPosition()
Main.delMouton(mouton)
del mouton
When I try to run main.py Python runs into an error and it basically says that the problem is coming from the fact that I am trying to import Simulation in Main but then I import Main in Simulation.
To understand a bit more about how import work I read this question and I am quite lost : when I run main.py what should happen is :
- Python reads the content from main and defines the Main class
- Python read the import Simulation statement and notices Simulation hasn't been imported yet
- So Python executes stuff in the simulation file, starting with the import Main statement
- When Python reads that line, it should notice that the Main class has been imported so it should continue reading content in the simulation file (am I wrong ?)
But it turns out that's not the case : when Python reads the import Main statement in the simulation file, it tries to run again the content in the main file, although it is already imported
I hope my explanation is clear