Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > module confusion

Reply
Thread Tools

module confusion

 
 
rjcarr
Guest
Posts: n/a
 
      10-02-2007
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.

So my question is ... why are they different? I mean, in terms of
designing these modules, how would you go about getting a sub-module
in your name space? And on the other side, how would you go about
getting it out?

Thanks!

 
Reply With Quote
 
 
 
 
mensanator@aol.com
Guest
Posts: n/a
 
      10-02-2007
On Oct 1, 10:03?pm, rjcarr <(E-Mail Removed)> wrote:
> 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'


What do suppose that message means?

>
> 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.
>
> So my question is ... why are they different?


Because you misspelled it. First, do a dir() on logging:

>>> import logging
>>> dir(logging)

['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR',
'FATAL', 'FileHandler', 'Filter', 'Filterer', 'Formatter', 'Handler',
'INFO', 'LogRecord', 'Logger', 'Manager', 'NOTSET', 'PlaceHolder',
'RootLogger', 'StreamHandler', 'WARN', 'WARNING', '__author__',
'__builtins__', '__date__', '__doc__', '__file__', '__name__',
'__path__', '__status__', '__version__', '_acquireLock',
'_defaultFormatter', '_handlerList', '_handlers', '_levelNames',
'_lock', '_loggerClass', '_releaseLock', '_srcfile', '_startTime',
'addLevelName', 'atexit', 'basicConfig', 'cStringIO', 'codecs',
'critical', 'currentframe', 'debug', 'disable', 'error', 'exception',
'fatal', 'getLevelName', 'getLogger', 'getLoggerClass', 'info', 'log',
'logProcesses', 'logThreads', 'makeLogRecord', 'os',
'raiseExceptions', 'root', 'setLoggerClass', 'shutdown', 'string',
'sys', 'thread', 'threading', 'time', 'traceback', 'types', 'warn',
'warning']

You can now pick any item from this list to further expand
with dir(), but notice "handlers" isn't one of them.

> I mean, in terms of
> designing these modules, how would you go about getting a sub-module
> in your name space? And on the other side, how would you go about
> getting it out?
>
> Thanks!



 
Reply With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      10-02-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Oct 1, 10:03?pm, rjcarr <(E-Mail Removed)> wrote:
>> 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'

>
> What do suppose that message means?
>
>> 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.
>>
>> So my question is ... why are they different?

>
> Because you misspelled it. First, do a dir() on logging:


No, he didn't. There is a logging.handlers module; it's just not imported by
importing logging.

OP: logging is a package and logging.handlers is one module in the package. Not
all of the modules in a package are imported by importing the top-level package.
os.path is a particularly weird case because it is just an alias to the
platform-specific path-handling module; os is not a package.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      10-02-2007
In message <(E-Mail Removed)>, Robert
Kern wrote:

> Not all of the modules in a package are imported by importing the
> top-level package.


You can't import packages, only modules.

> os.path is a particularly weird case because it is just an alias to the
> platform-specific path-handling module; os is not a package.


os is a module, os.path is a variable within that module. That's all there
is to it.
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      10-02-2007
On Tue, 02 Oct 2007 19:34:29 +1300, Lawrence D'Oliveiro wrote:

> In message <(E-Mail Removed)>, Robert
> Kern wrote:
>
>> Not all of the modules in a package are imported by importing the
>> top-level package.

>
> You can't import packages, only modules.


Oh come on, this is unnecessary nitpicking. Importing the module
`__init__` from a package using the name of the package is close enough to
justify the phrase "I import the package" IMHO.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      10-02-2007
Lawrence D'Oliveiro wrote:
> In message <(E-Mail Removed)>, Robert
> Kern wrote:
>
>> Not all of the modules in a package are imported by importing the
>> top-level package.

>
> You can't import packages, only modules.
>
>> os.path is a particularly weird case because it is just an alias to the
>> platform-specific path-handling module; os is not a package.

>
> os is a module, os.path is a variable within that module. That's all there
> is to it.


Yes, but os.path is also module. That's why I said it was a weird case.

In [1]: import os

In [2]: type(os.path)
Out[2]: <type 'module'>

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      10-02-2007
Lawrence D'Oliveiro wrote:
> In message <(E-Mail Removed)>, Robert
> Kern wrote:
>
>> Not all of the modules in a package are imported by importing the
>> top-level package.

>
> You can't import packages, only modules.
>
>> os.path is a particularly weird case because it is just an alias to the
>> platform-specific path-handling module; os is not a package.

>
> os is a module, os.path is a variable within that module. That's all there
> is to it.


The Python documentation could probably be a little more expansive on
the import front, but it's an area where historically many different
attempts have been made to fix up the problems, or create new and more
usable layers [that suffer from different problems].

You *can* import a package, and a package is just a *little* different
from a module in that the __init__.py file in the package directory
provides the source for the first-import execution instead of the
modulename.py file. The global namespace during this process is the
package's global namespace, so if further imports are executed by the
__init__ module the names of the imported objects are created in the
package global namespace (along with the names of defined classes and
functions, and other names bound by assignment) and can therefore be
referenced relative to it. The package in which the package import was
executed will end up defined in the namespace in which the import
statement was executed.

I will grant that you can't basically do anything in the __init__.py
that you can't do in a module's modulename.py file. It is possible to
define a hierarchy of namespaces concordant with the package's directory
structure, but it doesn't matter whether package import creates the
hierarchical namespace or assignment does. You can access module
namespaces in a hierarchical way.

That's about all that can be said without delving into the murky land of
relative vs. absolute imports. That can be for another day. And another
writer, probably.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline

 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      10-03-2007
In message <(E-Mail Removed)>, Robert
Kern wrote:

> Lawrence D'Oliveiro wrote:
>
>> In message <(E-Mail Removed)>, Robert
>> Kern wrote:
>>
>>> Not all of the modules in a package are imported by importing the
>>> top-level package.

>>
>> You can't import packages, only modules.
>>
>>> os.path is a particularly weird case because it is just an alias to the
>>> platform-specific path-handling module; os is not a package.

>>
>> os is a module, os.path is a variable within that module. That's all
>> there is to it.

>
> Yes, but os.path is also module. That's why I said it was a weird case.


You can't have modules within modules. os.path isn't an exception--see
below.

> In [1]: import os
>
> In [2]: type(os.path)
> Out[2]: <type 'module'>


On my Gentoo system:

>>> import os
>>> os.path

<module 'posixpath' from '/usr/lib64/python2.5/posixpath.pyc'>

It's just a variable that happens to point to the posixpath module.
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      10-03-2007
In message <(E-Mail Removed)>, Steve
Holden wrote:

> You *can* import a package ...


You're right. I was misremembering the behaviour of PyCrypto, where
importing the upper-level packages do little more than give you a list of
what algorithms are available.
 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      10-03-2007
Lawrence D'Oliveiro <(E-Mail Removed)_zealand> writes:

> On my Gentoo system:
>
> >>> import os
> >>> os.path

> <module 'posixpath' from '/usr/lib64/python2.5/posixpath.pyc'>
>
> It's just a variable that happens to point to the posixpath module.


There's no "pointing" going on. It's another name bound to the same
object, of equal status to the 'posixpath' name.

Python doesn't have pointers, and even "variable" is a misleading term
in Python. Best to stick to "name" and "bound to".

--
\ "Crime is contagious ... if the government becomes a |
`\ lawbreaker, it breeds contempt for the law." -- Justice Louis |
_o__) Brandeis |
Ben Finney
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
confusion about package/module imports Jugdish Python 3 01-01-2008 11:16 PM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
semi-newbie module namespace confusion pnau@sjm.com Python 6 10-07-2005 08:01 AM
Catalyst 6k Series Module Confusion Mike Hanson Cisco 3 03-07-2004 06:20 PM
re module substitution confusion Doug Farrell Python 1 07-08-2003 05:25 PM



Advertisments