473,396 Members | 2,018 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,396 software developers and data experts.

confusion about package/module imports

Why doesn't the following work?
>>ls $HOME
$HOME/pkg/__init__.py
$HOME/pkg/subpkg/__init__.py
$HOME/pkg/subpkg/a.py
$HOME/pkg/subpkg/b.py
>>cat $HOME/pkg/__init__.py
# empty
>>cat $HOME/pkg/subpkg/__init__.py
import a
import b
>>cat $HOME/pkg/subpkg/a.py
class A:
pass
>>cat $HOME/pkg/subpkg/b.py
import pkg.subpkg.a
class B(pkg.subpkg.a.A):
pass
>>setenv PYTHONPATH $HOME:$PYTHONPATH
python $HOME/pkg/subpkg/b.py
Traceback (most recent call last):
File "pkg/subpkg/b.py", line 1, in ?
import pkg.subpkg.a
File "$HOME/pkg/subpkg/__init__.py", line 2, in ?
import b
File "$HOME/pkg/subpkg/b.py", line 2, in ?
class B(pkg.subpkg.a.A):
AttributeError: 'module' object has no attribute 'subpkg'
Jan 1 '08 #1
3 1438
Jugdish wrote:
Why doesn't the following work?
...
[well boiled-down code skipped]
>>>setenv PYTHONPATH $HOME:$PYTHONPATH
python $HOME/pkg/subpkg/b.py
Traceback (most recent call last):
File "pkg/subpkg/b.py", line 1, in ?
import pkg.subpkg.a
File "$HOME/pkg/subpkg/__init__.py", line 2, in ?
import b
File "$HOME/pkg/subpkg/b.py", line 2, in ?
class B(pkg.subpkg.a.A):
AttributeError: 'module' object has no attribute 'subpkg'
OK, here's a trick for finding import problems:
python -v <file to fiddle>
(shows all imports)

And for this case:
sprinkle prints to find out what is happening.

so, add "print __name, __file__" to the top of each file where
you wonder what is going on.
I later added prints in pkg/subpkg/__init__.py to make the steps clear.

You'll see that b is executed (making module __main__),
it imports pkg.subpkg.a,
which is accomplished by importing pkg (successfully),
then by importing pkg.subpkg
which imports pkg.subpkg.a (successfully)
and then imports pkg.subpkg.b
which then attempts to import pkg.subpkg.a
At that point, only the module pkg
and what should eventually become pkg.subpkg.a
have been successfully imported. pkg.subpkg had not yet been imported.
If you remove the "import b" from subpkg's __init__, you will find
your problems going away.
Alternatively, you can remove the import a / import b from subpkg
and add import subpkg.a, subpkg.b to pkg's __init__. Essentially,
you need pkg.subpkg fully imported before you import pkg.subpkg.b

Of course, in most of these cases you will have imported the code
for b twice, once as a main program, and once as a module in the
hierarchy, which is probably your actual problem (and why I use
"print __name__, __file__").
--Scott David Daniels
Sc***********@Acm.Org
Jan 1 '08 #2
Thanks very much for your helpful response!
You'll see that b is executed (making module __main__),
(1) it imports pkg.subpkg.a,
(2) which is accomplished by importing pkg (successfully),
(3) then by importing pkg.subpkg
(4) which imports pkg.subpkg.a (successfully)
(5) and then imports pkg.subpkg.b
(6) which then attempts to import pkg.subpkg.a
What I'm not really understanding here is why this fails at lines (5)
and (6). If pkg.subpkg.a has already been successfully imported at
line (4), then (6) should be detected as a duplicate import and just
be skipped, right? So the import at line (5) should succeed.
Jan 1 '08 #3
Jugdish wrote:
Thanks very much for your helpful response!
>You'll see that b is executed (making module __main__),
(1) it imports pkg.subpkg.a,
(2) which is accomplished by importing pkg (successfully),
(3) then by importing pkg.subpkg
(4) which imports pkg.subpkg.a (successfully)
(5) and then imports pkg.subpkg.b
(6) which then attempts to import pkg.subpkg.a

What I'm not really understanding here is why this fails at lines (5)
and (6). If pkg.subpkg.a has already been successfully imported at
line (4), then (6) should be detected as a duplicate import and just
be skipped, right? So the import at line (5) should succeed.
I'm sorry, I used shorthand. While a module is being imported,
it only provisionally has a name. Until subpkg is fully imported,
there is no module named pkg.subpkg. At the root level (pkg, for
example), the module is provisionally added. Further down the tree,
the module (such as that for pkg/subpkg/__init__.py) is only added
to the symbol table (the packages __dict__) when the module has been
completely imported.

-Scott
Jan 1 '08 #4

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

Similar topics

2
by: Mathias Waack | last post by:
Hi, I've got a problem with Fredrik's squeeze tool. I've got a script which accesses a Oracle DB with the help of DCOracle. This script works very well, but after squeezing it the "import...
4
by: Robert Ferrell | last post by:
I have a question about how to organize modules in a package. I think there is something fundamental that I am missing. Say I'm creating a package, GreatPackage, which has three sub-packages,...
4
by: Peter L. Buschman | last post by:
I have a package tree laid out like this foo/bar/module/submodule foo/bar/module/submodule/test foo/bar/module/submodule/test/test1.py foo/bar/module/submodule/test/test2.py .... What I...
0
by: Mark English | last post by:
Basic problem: If there is a C-extension module in a package and it tries to import another python module in the same package without using the fully qualified path, the import fails. Config:...
4
by: David Pratt | last post by:
Hi. I have code that currently depends on a particular package of a framework. I have decided I want to create my own package because I have made many changes and it is getting too difficult to...
1
by: Epetruk | last post by:
Hello, I have a solution with two projects. One of the projects is called MyProj with a root namespace called MyProj.Obj. The single source (vb) file for MyProj has a class called Obj....
4
by: Martin Blais | last post by:
Hi I'm a tad confused over a problem involving cycles between packages. Assume the following sets of files:: driver.py a/__init__.py a/alice.py
40
by: rjcarr | last post by:
Sorry if this is a completely newbie question ... I was trying to get information about the logging.handlers module, so I imported logging, and tried dir(logging.handlers), but got: ...
7
by: alito | last post by:
Hi all, I am new to using packages to group my modules. I can't figure out how to run a module that uses relative imports without writing a wrapper that imports that module. Everything I try...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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...

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.