473,387 Members | 1,693 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 problems in packages


Hello all,
I have an ImportError problem, which is probably correct,
but I do not understand it. And how to fix it...
Here is the basic structure of the code (I have reduced it first).

ROOT:
/main.py
/Handlers/__init__.py (empty)
/Handlers/Handlers.py
/Handlers/HandlerFactory.py
/Handlers/Default/__init__.py (empty)
/Handlers/Default/Handlers.py
/Handlers/Default/HandlerFactory.py

Now, for the content of the files:

ROOT/main.py contains:
def main():
command = 'A'
from Handlers.HandlerFactory import HandlerFactory
handler = HandlerFactory().makeHandler(command)

main()
ROOT/Handlers/Handlers.py contains:

class HandlerBase(object):
pass
ROOT/Handlers/HandlerFactory.py contains:

def HandlerFactory():
import Handlers.Default.HandlerFactory
return Handlers.Default.HandlerFactory.HandlerFactory()
ROOT/Handlers/Default/Handlers.py contains:

from Handlers.Handlers import HandlerBase

class Handler(HandlerBase):
pass

and finally:

ROOT/Handlers/Default/HandlerFactory.py contains:

from Handlers.Default.Handlers import Handler

class HandlerFactory(object):

def makeHandler(self,command):
print 'HERE'
return Handler()


.... when I start main.py, I get:

Traceback (most recent call last):
File "main.py", line 8, in ?
main()
File "main.py", line 5, in main
handler = HandlerFactory().makeHandler(command)
File "c:\ROOT\Handlers\HandlerFactory.py", line 6, in HandlerFactory
import Handlers.Default.HandlerFactory
ImportError: No module named Default.HandlerFactory
Using a bit different structure (with relative imports), I also had an
ImportError in ROOT/Handlers/Default/Handlers.py
I guess there is something I do not understand in the way Python looks for
the correct file in a package...

Can someone give me some explanations about what is happening here ?
And a way to solve that ?

Thanks for any help,


--
Stéphane Ninin
st*****@alussinan.org

Sep 28 '05 #1
3 1517
Also sprach Stéphane Ninin :


... when I start main.py, I get:

Traceback (most recent call last):
File "main.py", line 8, in ?
main()
File "main.py", line 5, in main
handler = HandlerFactory().makeHandler(command)
File "c:\ROOT\Handlers\HandlerFactory.py", line 6, in HandlerFactory
import Handlers.Default.HandlerFactory
ImportError: No module named Default.HandlerFactory


I forgot to mention: I am using Python 2.4.1

Sep 28 '05 #2
Stéphane Ninin wrote:
Traceback (most recent call last):
File "main.py", line 8, in ?
main()
File "main.py", line 5, in main
handler = HandlerFactory().makeHandler(command)
File "c:\ROOT\Handlers\HandlerFactory.py", line 6, in HandlerFactory
import Handlers.Default.HandlerFactory
ImportError: No module named Default.HandlerFactory


Are you sure that's right? You asked it to import
Handlers.Default.HandlerFactory but it is apparently importing just
Default.HandlerFactory instead? This seems unexpected to me, though
perhaps it can happen in certain situations. It's a hint, anyway.

Are the earlier four lines which you did show us relevant? (i.e. if
that's line six, what was on lines one, two, three, and four?)

-Peter
Sep 28 '05 #3
Stéphane Ninin wrote:
Also sprach Stéphane Ninin :
Sollte es denn möglich sein! Dieser alte Heilige hat in seinem Walde noch
Nichts davon gehört... that intra-package import takes precedence over
absolute import!
Here is the basic structure of the code (I have reduced it first).
And nicely so. Not letting similar names for modules, packages and classes
abound might have been a good idea, too.
I have an ImportError problem, which is probably correct,
but I do not understand it. And how to fix it... ROOT:
/main.py
/Handlers/__init__.py*(empty)
/Handlers/Handlers.py
/Handlers/HandlerFactory.py
/Handlers/Default/__init__.py*(empty)
/Handlers/Default/Handlers.py
/Handlers/Default/HandlerFactory.py ROOT/main.py contains:
from*Handlers.HandlerFactory*import*HandlerFactory
works and triggers
ROOT/Handlers/HandlerFactory.py contains:
import*Handlers.Default.HandlerFactory


which tries to import

ROOT/Handlers/Handlers/Default/HandlerFactory.py, but unfortunately
ROOT/Handlers/Handlers[.py] is not a package and therefore doesn't contain
a Default package. Currently, when you have

amodule.py
apackage/amodule.py
apackage/anothermodule.py

and try to import amodule into anothermodule, there is no way to tell Python
that you mean amodule.py and not apackage/anmodule.py.

Peter

Sep 28 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Manlio Perillo | last post by:
Hi. I'm a new user of Python but I have noted a little problem. Python is a very good language but it is evolving, in particular its library is evolving. This can be a problem when, ad example,...
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...
2
by: Bill Jackson | last post by:
Once again, I am having issues with imports... Until now, I thought the general guidelines were to rarely use 'from x import y' syntax, except when you really want to copy names over. However, I...
5
by: luca72 | last post by:
Hello i can't import cherrypy2 but i don't know why this is the sys path: '', '/home/pirataja/opo.net/python/lib/python2.5/site-packages/ setuptools-0.6c7-py2.5.egg',...
3
by: jrh | last post by:
Hello, From previous posts and documentation it seems python should be able to import a module that is compiled into a .dll just as well as a .pyd. I have a pyd that works fine, but after...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.