473,473 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question about importing modules

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.

Jul 18 '05 #1
3 1416
py****@bag.python.org 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

Jul 18 '05 #2
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
Jul 18 '05 #3
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".
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
2
by: Gabriele *darkbard* Farina | last post by:
Hi, I'm using Python 2.5 to develop a simple MVC framework based on mod_python. To load my controllers, I create new modules using the "new" module like this: # .... my_module =...
14
by: T. Crane | last post by:
Hi, When troubleshooting code that's saved in a text file, I often find that I want to make a change to it, re-save it, then reimport it. However, just typing import myTestCode doesn't...
0
by: Martin P. Hellwig | last post by:
Hello all, I had some troubles in the past how to arrange my packages and modules, because I usually don't develop my stuff in the Lib\site-packages directory I have some troubles when importing...
7
by: Hussein B | last post by:
Hey, Suppose I have a Python application consists of many modules (lets say it is a Django application). If all the modules files are importing sys module, how many times the sys module will be...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.