4

I am trying to make a python script that works so:

I've got a main file that import another file, which is a class with functions inside it. The main file calls a function in the module, here it works all well, but when the def inside the module calls another def in the main again, I get an error.

For example, in main.py I've got this

from module import *
module = Example()

def foo():
 module.foo2()

def foo3():
 print "here is the problem"

if(__name__ == "__main__"):
 foo()

In module.py I've got:

class Example():
 def foo2(self):
  foo3()

foo2 is called perfectly, but when I try to call foo3() I get a NameError (global name 'foo3' is not defined). I know I should import it somehow but I don't know how to do it.

Last thing: I am quite new to python so please explain good :-) Thanks

1
  • I assume if(if __name__ == "__main__"): is a typo, and the outer if(...) should be removed? Commented Mar 24, 2014 at 17:04

3 Answers 3

3

I would avoid creating circular dependencies and refactor your code such that foo3 is either in module, or another new module. For example (I cleaned things up a little bit as I went along to follow PEP8):

main.py

import module
example = module.Example()

def foo():
 module.foo2()

if __name__ == "__main__":
 foo()

module.py

import module3

class Example():
    def foo2(self):
        module3.foo3()

module3.py

 def foo3():
     print "here is the problem"

If you absolutely must keep the circular dependancy, then the best way to handle it would be to move the import in module.py to the end of the file as suggested on effbot. Again, I would avoid doing this at all cost.

class Example():
 def foo2(self):
  main.foo3()

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

7 Comments

Wow! So many answers! I'll try this too and see what's happen!
The above only works for me if I use: from main import foo3 at the bottom of module.
Fixed. Thanks for catching that. IMHO, it would be preferred to use import main, then main.foo3() rather than from main import foo3. To quote the Zen of Python "Namespaces are one honking great idea -- let's do more of those!".
yes, for me too. But if you want to use it in the way he explain you can also write main.foo3() instead of foo3()
Haha same explanation :-) However, you answer is what I was looking for! Thanks!
|
1

You want foo3 in main to be called from module? This will work in the module:

class Example():
    def foo2(self):
        from main import foo3
        foo3()

However, I'm not sure what this buys you in module (how useful such a circular dependency is), since the two modules now tightly depend on each other. I imagine you want the Example class to be able to use the foo3 function from any caller.

I would do something like pass the function into the class:

from module import *
module = Example()

def foo():
    module.foo2(foo3)

def foo3():
    print "here is the problem"

if __name__ == "__main__":
    foo()

Then in module.py:

class Example():
    def foo2(self, myFoo):
        myFoo()

Comments

0

Sorry about the late reply, but I've found a very elegant, albeit 'hackish' solution:

In you main module you can add this: (No need for the recursive imports...)

module.foo3 = foo3

It has the effect that foo3 is now a defined thing in module.py, even though you have never declared it within that file! Try it. It works.

I encountered this situation where I want to put my GUI interface in another file, but the GUI obviously has to call functions in the main module. So I found this solution, out of desperation :)

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.