I'm trying to create a module that requires variables from the main class, so I imported said variables to the module, but when I try to test my new module by importing it into the main class, it says it can't import it.
It seems to be BECAUSE I'm importing the main class in the new module that causes the issue because whenever I remove the import, it works but it can no longer access the variables from the main class needed to function.
The main class:
from Mod import Mod
variable1=5
variable2=3
mod=Mod()
mod.task()
The new module:
from Main import variable1, variable2
class Mod:
def task(self):
print(variable1+variable2)
When I run the main class I get this:
Traceback (most recent call last):
File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Main.py", line 1, in <module>
from Mod import Mod
File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Mod.py", line 1, in <module>
from Main import variable1, variable2
File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Main.py", line 1, in <module>
from Mod import Mod
ImportError: cannot import name 'Mod' from 'Mod' (D:\.here\Computer Science\Computer Science Stuff\Python Projects\Mod.py)
And when I run the new module I get this:
Traceback (most recent call last):
File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Mod.py", line 1, in <module>
from Main import variable1, variable2
File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Main.py", line 1, in <module>
from Mod import Mod
File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Mod.py", line 1, in <module>
from Main import variable1, variable2
ImportError: cannot import name 'variable1' from 'Main' (D:\.here\Computer Science\Computer Science Stuff\Python Projects\Main.py)
I have no idea why this might happen. It contradicts what I've been doing in Java.
How would I be able to reference global variables that are stored in the main class if not through importing them?