469,303 Members | 1,828 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,303 developers. It's quick & easy.

import error between 2 modules

I am new to python,and am learning from the tutorials
i created 2 .py files like below and put the main in one of them

empmodule.py
----------
from workmodule import Worker

class Employer:
def __init__(self,n):
self.name=n
self.worker=Worker()
def getemployerName(self):
return self.name
def callWorker(self,message):
self.worker.answerCall(message)
if __name__ == "__main__":
emp=Employer()
emp.callWorker("report to work")
workmodule.py
------------------
from empmodule import Employer
class Worker:
def __init__(self):
self.emp=Employer()
def answerCall(self,msg):
print "Worker :"+msg+" received
from :"+self.emp.getemployerName()
is this kind of mutual import not allowed in python?
I am getting
"from workmodule import Worker
ImportError: cannot import name Worker"

any advice/pointers most welcome
thanks
jim
Aug 27 '08 #1
4 1593
jimgardener wrote:
I am new to python,and am learning from the tutorials
i created 2 .py files like below and put the main in one of them

empmodule.py
----------
from workmodule import Worker

class Employer:
def __init__(self,n):
self.name=n
self.worker=Worker()
def getemployerName(self):
return self.name
def callWorker(self,message):
self.worker.answerCall(message)
if __name__ == "__main__":
emp=Employer()
emp.callWorker("report to work")
workmodule.py
------------------
from empmodule import Employer
class Worker:
def __init__(self):
self.emp=Employer()
def answerCall(self,msg):
print "Worker :"+msg+" received
from :"+self.emp.getemployerName()
is this kind of mutual import not allowed in python?
I am getting
"from workmodule import Worker
ImportError: cannot import name Worker"

any advice/pointers most welcome
thanks
You are doing a circular import. Here is some things you can do:
#1. import the module names, not definitions inside them. For example:

import empmodule
.....
self.emp = empmodule.Employer()
#2. place both classes in the same file


By the way, your program must semantically be wrong. When you create an
Employer instance from your main program, it will try to create a worker
in its constuctor:

self.worker = workmodule.Worker()

When the worker is being created, it tries to create an employer in its
constuctor:

self.emp = empmodule.Employer()

These constructors will be calling each other forever. This will be an
infinite recursion. In the end, no constructor call will be finished and
you will not create any objects but reach recursion limit instead.

Best,

Laszlo

Aug 27 '08 #2
Le Wednesday 27 August 2008 12:38:33 jimgardener, vous avez écrit*:
empmodule.py
----------
from workmodule import Worker

class Employer:
* * def __init__(self,n):
* * * * self.name=n
* * * * self.worker=Worker()
* * def getemployerName(self):
* * * * return self.name
* * def callWorker(self,message):
* * * * self.worker.answerCall(message)
if __name__ == "__main__":
* * emp=Employer()
* * emp.callWorker("report to work")
workmodule.py
------------------
from empmodule import Employer
class Worker:
* * def __init__(self):
* * * * self.emp=Employer()
* * def answerCall(self,msg):
* * * * print "Worker :"+msg+" received
from :"+self.emp.getemployerName()
For this case you could do your import directly in the __init__ func and watch
the infinite loop you did in action !

empmodule.py
----------
from workmodule import Worker

class Employer:
def __init__(self,n):
self.name=n
self.worker=Worker(self)
def getemployerName(self):
return self.name
def callWorker(self,message):
self.worker.answerCall(message)
if __name__ == "__main__":
emp=Employer()
emp.callWorker("report to work")
workmodule.py
------------------
class Worker:
def __init__(self, employer):
from empmodule import Employer
if not isinstance(employer, Employer):
raise ValueError("Not an employer")
self.emp=employer()

--
_____________

Maric Michaud
Aug 27 '08 #3
On Aug 27, 5:37 pm, Laszlo Nagy <gand...@shopzeus.comwrote:
These constructors will be calling each other forever. This will be an infinite recursion.
sorry,that was a quite stupid mistake on my part..thanks for pointing
out..
jim
Aug 27 '08 #4
jimgardener wrote:
is this kind of mutual import not allowed in python?
it is, but you need to understand how things work before you can use it
without getting yourself into trouble. this page might help:

http://effbot.org/zone/import-confusion.htm

see the "Circular Import" section for a discussion of what's going on
here, and "Which Way Should I Use" for some guidelines (pay special
attention to the last item in that list ;-)

</F>

Aug 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Markus Doering | last post: by
5 posts views Thread by Steve Holden | last post: by
1 post views Thread by mirandacascade | last post: by
23 posts views Thread by Shane Hathaway | last post: by
49 posts views Thread by Martin Unsal | last post: by
10 posts views Thread by Thomas Guettler | last post: by
reply views Thread by Murat Gunduz | last post: by
4 posts views Thread by bvdp | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.