[Python-bugs-list] [ python-Bugs-493628 ] import not pythonic in 2.1.1

noreply@sourceforge.net noreply@sourceforge.net
Sun, 16 Dec 2001 10:03:26 -0800


Bugs item #493628, was opened at 2001-12-15 03:37
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=493628&group_id=5470

Category: Python Interpreter Core
Group: Not a Bug
Status: Closed
Resolution: Wont Fix
Priority: 5
Submitted By: Chris Withers (fresh)
Assigned to: Nobody/Anonymous (nobody)
Summary: import not pythonic in 2.1.1

Initial Comment:
Take a package 'aPackage', which contains a sub-package 'aSubPackage' that in turn contains a 
module 'aModule' that defines a class 'aClass'.

If I do:

import aPackage.aSubPackage
print aPackage.aSubPackage.aModule

I get:

Traceback (most recent call last):
  File "x.py", line xx, in ?
    print aPackage.aSubPackage.aModule
AttributeError: 'aPackage.aSubPackage' module has no attribute 'aModule'

A yet, if I do:

import aPackage.aSubPackage.aModule
print aPackage.aSubPackage.aModule

I get:
<module 'aPackage.aSubPackage.aModule' from 'some_path'>

...as expected, which is very confusing and doesn't feel 'right' :-S




----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2001-12-16 10:03

Message:
Logged In: YES 
user_id=31435

Chris, why do you think you need to use an exec stmt?  You 
haven't shown an example of why it's needed, and it sure 
isn't obvious.

Modules must be imported, and importing a module M requires 
an import stmt whose last path component is M -- that's 
about all there is to it.

----------------------------------------------------------------------

Comment By: Chris Withers (fresh)
Date: 2001-12-16 04:58

Message:
Logged In: YES 
user_id=24723

Well, I read it, and it explains the history, but it doesn't give reasons, particularly for:

> Contrarily, when using syntax like import item.subitem.subsubitem, each item except for the last must be 
a package; the last item can be a module
> or a package but can't be a class or function or variable defined in the previous item. 

...or for the above stuff.

But, while I find it counter-intuitive (and means I have to use unpleasant exec statements), I am but one 
voice among many who probably feel differently so I'll just accept it as one of the things i don't like about 
python...

thanks for your time :-)

Chris


----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-12-15 09:46

Message:
Logged In: YES 
user_id=6380

http://www.python.org/doc/essays/packages.html

----------------------------------------------------------------------

Comment By: Chris Withers (fresh)
Date: 2001-12-15 09:15

Message:
Logged In: YES 
user_id=24723

Okay, if it's intended, that means there's a good reason for it...

...where can I look for enlightenment as to what that is?

cheers,

Chris

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-12-15 07:39

Message:
Logged In: YES 
user_id=6380

Better adjust your intuition. :-)

This is as intended. Attributes of packages that are
*modules* are only loaded when the corresponding module is
explicitly imported (by you, or by some other code, e.g. the
package's __init__.py).

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=493628&group_id=5470