Question about importing modules

Discussion in 'Python' started by pythos, Aug 21, 2004.

  1. pythos

    pythos Guest

    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.
     
    pythos, Aug 21, 2004
    #1
    1. Advertising

  2. pythos

    Ken Beesley Guest

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

    What you want is

    from os import utime

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

    Ken
     
    Ken Beesley, Aug 21, 2004
    #2
    1. Advertising

  3. pythos

    Peter Hansen Guest

    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
     
    Peter Hansen, Aug 22, 2004
    #3
  4. On Sat, 21 Aug 2004 14:59:36 -0400, 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.


    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.
    >>> import os
    >>> os

    <module 'os' from '/usr/lib/python2.3/os.pyc'>
    >>> dir(os)

    ['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".
     
    Jeremy Bowers, Aug 22, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tobiah
    Replies:
    2
    Views:
    312
    Tobiah
    Sep 14, 2003
  2. plb
    Replies:
    2
    Views:
    354
  3. warhero

    importing modules question

    warhero, Oct 18, 2007, in forum: Python
    Replies:
    2
    Views:
    289
    Amit Khemka
    Oct 18, 2007
  4. Peter Peyman Puk

    Importing v reloading modules modules

    Peter Peyman Puk, Mar 19, 2010, in forum: Python
    Replies:
    0
    Views:
    300
    Peter Peyman Puk
    Mar 19, 2010
  5. cronoklee
    Replies:
    1
    Views:
    206
    shearichard
    Dec 17, 2010
Loading...

Share This Page