473,473 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

subpackage import problem

I'm having a problem with packages within packages. Here's an example:

foo/
foo/__init__.py: empty file
foo/sub/__init__.py:
from foo.sub.B import B
foo/sub/A.py:
class A:
pass
foo/sub/B.py
import foo.sub.A
class B(foo.sub.A):
pass

Trying to "import foo.sub" will result in this error:
import foo.sub

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "foo/sub/__init__.py", line 1, in ?
from foo.sub.B import B
File "foo/sub/B.py", line 3, in ?
class B(foo.sub.A):
AttributeError: 'module' object has no attribute 'sub'

This can be fixed using a relative import in B.py, but I don't feel
comfortable with relative package imports (unintentional shadowing of
modules you want to use in the future, and the symantics might change in
the future due to PEP 328).

Does anyone have any suggestions?

The situation this comes up is:
- Have package foo with module bar.
- Module bar has a large number of classes.
- I want to break bar up into multiple files because it is getting too
large.
- In order to keep these classes together, I create a subpackage with a
separate file for each class.
- I import these files in the subpackage __init__.py so I don't have to
change any other code and so I don't have
foo.bar.flashlight.flashlight() lines where the word
"flashlight" is repeated.

-Eric
Aug 21 '05 #1
0 1034

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

Similar topics

0
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a...
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...
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
2
by: Panard | last post by:
Hi, I'm experiencing a strange problem while trying to manage a ftp connection into a separate thread. I'm on linux, python 2.4.3 Here is a test : ------ ftp_thread.py ------ import ftplib...
0
by: Derek Peschel | last post by:
Can I add my own subpackages to modules that are written in C? I've put /Users/dpeschel/lib/python into my PYTHONPATH variable. Inside there, I've created a termios directory, and inside termios...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
0
by: Victor Lin | last post by:
Hi, I got some problem with import. I have some python file that should be executed. Something like unit-test or other small tool. The project is not so small. So I want to separate these files...
5
by: Stef Mientki | last post by:
hello, The import statement "import sqlite3" gives the error given below. In simple programs, the import statement (sometimes) succeed, and I can indeed access the database. So I guess there is...
13
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My...
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,...
1
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...
0
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.