beginner import confusion

D

DilbertFan

Hi,
I really thought that I had this importing business down.

I recently downloaded and installed mx module for the DateTime class.

In IDLE, I go:

import mx
mx.DateTime.DateTime(2004)

I get AttributeError: 'module' object has no attribute 'DateTime'

but if you type:
import mx.DateTime
mx.DateTime.DateTime(2004)
<DateTime object for '2004-01-01 00:00:00.00' at a979e0>

If you "import mx", like the first example, doesn't that get everything?

If you know, from the docs, that it contains a DateTime class and then a
DateTime object, and you specify the whole thing, why shouldn't that work?
thanks for your patience
Steve
 
S

Scott David Daniels

DilbertFan said:
Hi,
I really thought that I had this importing business down.
I recently downloaded and installed mx module for the DateTime class.

In IDLE, I go:
import mx
mx.DateTime.DateTime(2004)
> I get AttributeError: 'module' object has no attribute 'DateTime'

mx is a package, mx.DateTime is a module; you need to import a module
to get to its contents.
but if you type:
import mx.DateTime
mx.DateTime.DateTime(2004)
<DateTime object for '2004-01-01 00:00:00.00' at a979e0>
> If you "import mx", ... doesn't that get everything?
If mx were a module, you would be right. However, it is a "package,"
a collection of modules and packages. Packages are a way of keeping
the names one provider of many python modules from conflicting with
the module names of both you and python itself.
If you know, from the docs, that it contains a DateTime class
and then a DateTime object,
mx.DateTime.DateTime is a class, not an object.
You could do:
from mx.DateTime import DateTime
to simply get the DateTime class and then use DateTime(2004).
 
D

DilbertFan

packages, okay... , yes, thank you.. got it, back to one of my O'Reilly
books
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,969
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top