[triangle-zpug] how to make import work like execfile() ?

David Handy david at handysoftware.com
Thu Dec 1 20:50:18 CET 2005


First, no offense intended, I *like* the fact that import defines a level of
scope. I like knowing where every name in my code was defined, just by
highlighting it and doing a search within one file. A wrap-around search on
any name will eventually lead to an assignment statement, def statement,
class statement, or import statement, unless it is one of a limited set of
well-documented built-in functions or keywords. And all of the built-in
names are highlighted by most programming editors. Everything is nice and
clear.

But if you really want to dump all of the symbols from a module into your
current scope, then you can do something like:

from Tkinter import *

(Substitute your desired module for Tkinter.) I don't even teach this
technique in my beginning Python book. In my own code, when I don't want
to type out Tkinter.blah I do "import Tkinter as Tk" and then I am happy to
type Tk.blah. For other modules I either import the names explicitly ("from
cStringIO import StringIO") or just type out the module name if it is used
infrequently.

David H

On Thu, Dec 01, 2005 at 02:29:25PM -0500, Chris Kennedy wrote:
> 
> Is there an execfile() variant that will load pyc files?
> 
> I like execfile() since it makes the code appear to be defind "in place" 
> in the script and therefore be in the same scope. Using import or from 
> creates another level of scope. 
> 
> So I was wondering if a modification to import would make the module 
> contents by available in the main scope, plus it would work with pyc files.
> 
> any ideas? thanks
> 
> -- 
> Chris Kennedy                  http://www.coventor.com
> Product Manager                Ph: 919-854-7500 x156
> Cary, NC                       Fx: 919-854-7501
>                           Mobile: 919-414-6653
> 
> 
> 
> _______________________________________________
> triangle-zpug mailing list
> triangle-zpug at starship.python.net
> http://starship.python.net/mailman/listinfo/triangle-zpug
> 

-- 
David Handy
Computer Programming is Fun!
Beginning Computer Programming with Python
http://www.handysoftware.com/cpif/



More information about the triangle-zpug mailing list