[Tutor] lots of Python errors

calvin exnihilo@myrealbox.com
Thu Jul 17 17:47:39 2003


Jordan James wrote:
> I don't know why, but I seem to get a lot of errors running Python.  How 
> I came to use Python was through Blender, a freeware 3D package (that 
> uses Python) that you've probably heard of.  At first, I found that only 
> a few of the scripts that I downloaded for use in Blender worked, so I 
> thought it was a problem with Blender.  So I got the actual python 
> command line and GUI.  I still had lots of errors running scripts by 
> other people.  For example,  "os.system".   When I say
>  
> os.system("start notepad")
>  
> ,it says
>  
> Traceback (most recent call last):
>   File "C:/Program Files/Python/launch.py", line 1, in ?
>     os.system("start notepad")
> NameError: name 'os' is not defined
>  
> I'm pretty sure this isn't normal, because I read all of the tutor pages 
> by other people, and these things work for them.  I am running Python 
> 2.3; is this possibly a really old version?  Is there some major step 
> I'm not doing?  I'm running Windows 98, does Python have problems with 
> Windows?  Are there any other factors I need to consider?
>  
> I know my problem is pretty broad, but if anyone knows what is going on, 
> I would appreciate it if you told me.
>  
> Thanks.
>  
> Jordan

Hi Jordan,

The problem with your os.system line seems to be that you have not 
imported the module first. Python doesn't know what 'os' refers to 
(could be a module, a variable in another file, whatever), so it gives 
you the name error, because it doesn't recognize the name.

If you import the os module first, then it knows what 'os' refers to, 
and will correctly call the function 'system' that is in that module:

Python 2.2.1 (#1, Nov 10 2002, 18:40:13)
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> os.system("ls")
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
NameError: name 'os' is not defined
 >>> import os
 >>> os.system("ls")
afile.txt
0
 >>>

You have to import a module before you have access to anything in that 
module (like the 'system' function in the os module).

-calvin

btw, Python 2.3 is very new. I think the latest stable release is 2.2.3.