473,839 Members | 1,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Handle rFactory import HandlerFactory
handler = HandlerFactory( ).makeHandler(c ommand)

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

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

def HandlerFactory( ):
import Handlers.Defaul t.HandlerFactor y
return Handlers.Defaul t.HandlerFactor y.HandlerFactor y()
ROOT/Handlers/Default/Handlers.py contains:

from Handlers.Handle rs import HandlerBase

class Handler(Handler Base):
pass

and finally:

ROOT/Handlers/Default/HandlerFactory. py contains:

from Handlers.Defaul t.Handlers import Handler

class HandlerFactory( object):

def makeHandler(sel f,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(c ommand)
File "c:\ROOT\Handle rs\HandlerFacto ry.py", line 6, in HandlerFactory
import Handlers.Defaul t.HandlerFactor y
ImportError: No module named Default.Handler Factory
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*****@alussin an.org

Sep 28 '05 #1
3 1542
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(c ommand)
File "c:\ROOT\Handle rs\HandlerFacto ry.py", line 6, in HandlerFactory
import Handlers.Defaul t.HandlerFactor y
ImportError: No module named Default.Handler Factory


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(c ommand)
File "c:\ROOT\Handle rs\HandlerFacto ry.py", line 6, in HandlerFactory
import Handlers.Defaul t.HandlerFactor y
ImportError: No module named Default.Handler Factory


Are you sure that's right? You asked it to import
Handlers.Defaul t.HandlerFactor y but it is apparently importing just
Default.Handler Factory 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*(em pty)
/Handlers/Handlers.py
/Handlers/HandlerFactory. py
/Handlers/Default/__init__.py*(em pty)
/Handlers/Default/Handlers.py
/Handlers/Default/HandlerFactory. py ROOT/main.py contains:
from*Handlers.H andlerFactory*i mport*HandlerFa ctory
works and triggers
ROOT/Handlers/HandlerFactory. py contains:
import*Handlers .Default.Handle rFactory


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.p y

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
2766
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, a module change its interface or its implementation in a fundamental way (an example: wxPython). This, I think, can be resolved by allowing an user to explicitly say what version of a module it wants (sush as version numbers in Linux shared...
49
3955
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 isomorphism between filesystem and namespace, doesn't seem very well suited for big projects. However, I might not really understand the Pythonic way. I'm not sure if I have a specific question here, just a general plea for advice. 1) Namespace....
2
1420
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 have run into issues by following this guideline. So... 1) What is going wrong in the example below? 2) What is a good way to handle imports for packages w/subdirectories? Here is a sample directory structure:
5
1295
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', '/home/pirataja/opo.net/python/lib/ python2.5/site-packages/TurboGears-1.0.4.4-py2.5.egg', '/home/pirataja/ opo.net/python/lib/python2.5/site-packages/TurboKid-1.0.4-py2.5.egg', '/home/pirataja/opo.net/python/lib/python2.5/site-packages/ TurboJson-1.1.2-py2.5.egg',...
3
7275
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 renaming it to dll the import fails. Running python with -vv flag indicates it doesn't actually look for the dll (see below). Has dll import been defeatured in python? Thanks!
0
9697
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10650
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10295
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7830
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7019
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.