Question about importing modules

Peter Hansen peter at engcorp.com
Sun Aug 22 01:27:11 CEST 2004


pythos wrote:
> Newbie at python (but not programming) here...
> 
> I have a program that has "import os" at the top, and then later a call to
> utime() is made.  The python interpreter says "name 'utime' is not defined".
> But if I change "utime(...)" to "os.utime(...)" then it works fine.  Perhaps I
> am expecting the "import os" statement to work the same way as "import
> <package_name>.*" does in Java.  So is it the case that if I write "import os"
> in python, then I still need to write "os.utime(...)"?  Or is there something
> else wrong?  Thanks.

The equivalent to the Java version might be "from os import *", but
believe me, you do *not* want to do that, not most of the time in
Python, and never with the "os" module.

The usual approach is "import os" followed by "os.utime", as you
discovered.

"from os import utime" is also fine, though I think it's much
more common to do the former than this one, at least with
many or most of the builtin modules.

-Peter



More information about the Python-list mailing list