473,608 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:$PYTHONPA TH
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 1458
Jugdish wrote:
Why doesn't the following work?
...
[well boiled-down code skipped]
>>>setenv PYTHONPATH $HOME:$PYTHONPA TH
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***********@A cm.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
1648
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 DCOracle" fails. The DCOracle package consists of several python files and some shared libs. The import of one shared lib from within the package fails. It looks like a problem with the code responsible for searching the appropriate module file.
4
4185
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, myUtilities, GoodStuff, MoreGoodStuff. The directories look like: GreatPackage |
4
7274
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 want is for test1.py and test2.py to be able to import their parent "submodule" as part of a regression
0
1364
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: Python 2.4 on Windows 2000 For example: mypackage contains:
4
1393
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 maintain each time I retrieve an updated version of the framework from svn. The problem is, that there are all sorts of imports to the dependent package throughout my code and I just want to replace this module with something that will provide a...
1
1147
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. There is no
4
1826
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
3463
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: AttributeError: 'module' object has no attribute 'handlers' The only experience I have in modules is os and os.path ... if I do the same thing, simply import os and then type dir(os.path), it displays the contents as expected.
7
2181
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 it complains that I am attempting a relative import in a non-package. eg ~/python/testpackage$ ls config.py importer.py __init__.py
0
8063
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8498
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8478
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
8152
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,...
1
6014
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
3962
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...
0
4025
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
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
0
1331
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.