473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1691
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Markus Doering | last post by:
Hello, I just switched from 2.2 to Python 2.3. I am developing an XML/CGI interface to databases as a python package called "unitWrapper" containing several modules which ran fine under v2.2. ...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
23
by: Shane Hathaway | last post by:
Here's a heretical idea. I'd like a way to import modules at the point where I need the functionality, rather than remember to import ahead of time. This might eliminate a step in my coding...
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
49
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the...
3
by: kwatch | last post by:
What is the condition of module name which is available in 'from .. import ..' statement ? ---------------------------------------- import os print os.path # <module 'posixpath'...
10
by: Thomas Guettler | last post by:
If you look at this code, you see there are two kind of ImportErrors: 1. app_name has no attribute or file managment.py: That's OK. 2. managment.py exists, but raises an ImportError: That's not...
0
by: Murat Gunduz | last post by:
Dear list member, I am using a Linux machine (Fedora Core 7, 64 bit): Linux 2.6.21-1.3228.fc7 #1 SMP Tue Jun 12 14:56:37 EDT 2007 x86_64 I tried to compile a software (ncvtk) with the simple...
4
by: bvdp | last post by:
Terry Reedy wrote: <snip> <snip> <snip>
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.