Question about importing modules

P

pythos

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.
 
K

Ken Beesley

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.
What you want is

from os import utime

which will make 'utime' a name in the local namespace.

Ken
 
P

Peter Hansen

pythos said:
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
 
J

Jeremy Bowers

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.

Taking a slightly different tack than your other repliers which I think
might be more educational in the long run, pop open a Python shell and do
an "import os". That creates a "module" object in the current namespace
which you can manipulate. Example:

Python 2.3.4 (#1, Jun 8 2004, 17:41:43)
[GCC 3.3.3 20040217 (Gentoo Linux 3.3.3, propolice-3.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.['EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT
', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'E
X_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'F_OK', 'NGROUPS_MAX',
.... whole lotta other stuff ...
'stat', 'stat_float_times', 'stat_result', 'statvfs', 'statvfs_result', 'strerr
or', 'symlink', 'sys', 'sysconf', 'sysconf_names', 'system', 'tcgetpgrp', 'tcset
pgrp', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'ttyname', 'umask', 'uname', 'un
link', 'unsetenv', 'utime', 'wait', 'waitpid', 'walk', 'write']

"os" is an object that can be passed around like anything else. (Though I
have yet to ever have need to pass around a module object I can imagine
rare cases where it might be useful.)

That's why you can also use "import" in a function, and the module object
will be local to the function. (You can not use "from os import *" for
various technical reasons, but that's usually not useful anyhow.)

"from os import *" loads the "os" object, then walks it and loads its
contents into your local namespace. This is a particularly bad idea for
"os" because it has function names like "open", "path", "wait", "write",
and a lot of other function names you're likely to collide with. It is
rarely a good idea for code, though I will confess to sometimes using
"from constants import *" in large programs, leaning on the ALL_CAPS
convention to indicate "constantness".
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top