[Tutor] crash when importing random or sys module, in my embedded python

Lloyd Kvam pythontutor@venix.com
Wed Jun 18 19:25:04 2003


Are you importing a random.py in your current directory?

import actually executes the lines that start at the left margin.  These are
typically def, class, and import statements.  Some assignment statements are
also common.  Normally these simply create the classes and function definitions
that your program will be using.

If you import a script that simply starts doing things, opening files, reading,
writing, etc.  That will happen on the import.  import is only loosely analagous
to a C include or Cobol copy.

This is why a script that can be both imported or run usually is organized
with a "master" function and ends with something along the lines of:
if __name__ == '__main__':
	master()
When the script is being imported the __name__ is the name of module.  WHen it is
being run directly, __name__ is '__main__'.


R. Alan Monroe wrote:
> What kinds of things can go wrong during an import statement OTHER
> THAN an ImportError?
> 
> When I run this in my embedded python program, it takes the second
> option and writes "who knows" to the log. Sadly import sys crashes
> too, so I can't use sys.exc_info(). The weird thing is, import struct
> works like a champ?
> 
> 
> 
> 
> import struct
> 
> try:
>     import random
> except ImportError, e:
>     log = open('/badnews.txt', 'w')
>     log.write(e.args)
>     log.close()
> except:
>     log = open('/badnews.txt', 'w')
>     log.write("who knows\n")
>     log.close()
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582