noob import question

Diez B. Roggisch deets at nospam.web.de
Fri May 19 09:33:53 EDT 2006


> I have tried to look up what is going on, but I have not found
> anything.  Would it be possible for someone to take a minute and give
> an explanation?

The

from <module> import <*|nameslist>

syntax imports some or all names found in <module> into the current modules
namespace. Thus you can access your class.

But if you do 

import <module>

you only get <module> in your current namespace. So you need to access
anything inside <module> by prefixing the expression. In your case, it is

Student.Student

If you only write Student, that in fact is the MODULE Student, which
explains the error message.

Now while this sounds as if the from <module> import * syntax is the way to
go, you should refrain from that until you really know what you are doing
(and you currently _don't_ know), as this can introduce subtle and
difficult to debug bugs. If you don't want to write long module-names, you
can alias them:

import <moduel-with-long-name> as <shortname>


And it seems as if you have some JAVA-background, putting one class in one
file called the same as the class. Don't do that, it's a stupid restriction
in JAVA and should be avoided in PYTHON.

Diez



More information about the Python-list mailing list