0

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?

2
  • The source of the problem may be the recursive imports. You shouldn't make two files import each other Commented Oct 11, 2019 at 19:16
  • @JammyDodger do you mean circular imports? Commented Oct 11, 2019 at 19:22

5 Answers 5

3

Your issue here is called a "circular dependency". Main.py is trying to import a class from Mod.py, but before that can happen, Mod must import some variables from Main.py, but before that can happen...

Generally, the way to solve a circular dependency is to reorganize how your program is laid out. For instance, you might be able to parameterize Mod such that it no longer depends on Main. For example:

Main.py:

from Mod import Mod

variable1=5
variable2=3

mod=Mod()

mod.task(variable1, variable2)

Mod.py:

class Mod:
    def task(self, a, b):
        print(a + b)

Alternatively, you could store variable1 and variable2 in a different file. Finding the best solution will depend on what makes the most sense for your program.

Sign up to request clarification or add additional context in comments.

Comments

0

You could update Mod.task() to receive two extra parameters, so you'd get:

def task(self, a, b):
    print(a + b)

Then, in main, you would simply say:

mod.task(variable1, variable2)

No need for imports other than importing Mod into your main class

Comments

0

This is caused by the Circular dependency

As mentioned in the above answer, the way to solve a circular dependency is to reorganize how your program is laid out. Use parameters

Nevertheless, a simple workaround for this is by making inline imports

Main.py

from Mod import Mod

variable1=5
variable2=3

mod=Mod()

mod.task()

Mod.py


class Mod:
    def task(self):
        from Main import variable1, variable2
        print(variable1+variable2)

1 Comment

Although this code will run, the circular dependency is still present: both modules can still import each other, which results in unexpected behaviour. Your code actually prints the answer twice(!): once to import Main from Mod and once from actually running Main.
0

Follow the DRY principles. Do Not Repeat Yourself.

importing "Main" variables into Mod, is causing a circular dependency. Something you must avoid at all cost. Use Object Oriente Programming to solve your problem.

Steps to solve the problem:

  1. Delete the import of Main.
  2. Change task function to allow two arguments to be passed. It should be something like this

    def task(self, variable1, variable2):
       print(variable1 + variable2)
    

    #You can use any name for variables 1 and 2

  3. On the Main script, just call the function:

    Mod.task(variable1, variable2)
    

    Here you must use the name of the variables, because you are now calling the definition you gave in Main.

Comments

0

I don't understand what You mean in Java such things can be done.. But it is kind of possible in python respecting the rule that the bodies of the circuar-referencing modules must be able to execute without access to the other involved modules content.
So You can do:

# cat Main.py
import Mod
var1=2
var2=3
if __name__ == '__main__':
    mod=Mod.Mod()
    mod.task()
# cat Mod.py
import Main
class Mod(object):
    def task(self):
        print(Main.var1+Main.var2)

For example. Or handle the initialization of modules with functions You call after the import chain succeeded.
Anyway most of the time it is better to avoid such stuff an just initializie You classes and call functions with parameters.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.