how to include a file -- not import??

Andrew Csillag andrew at starmedia.net
Thu Jan 6 15:06:50 EST 2000


Darrell wrote:
> 
> from filename import *

Actually, that doesn't do it either if the other module wants to
reference vars in the importing namespace.

The way to make it so that the other code can access variables in the
including files namespace is:
exec open('filename').read() 

> 
> Doesn't create another namespace. Avoid this in most cases.
> 
> Use import filename and filename.someVar the "from" was intended to help
> when your running Python interactively.
> 
> --
> --Darrell
> "John Nielsen" <nielsenjf at my-deja.com> wrote in message
> news:8515bs$2vn$1 at nnrp1.deja.com...
> > Hopefully, there's a simple answer to a problem I am having.
> >
> > I am using import to include a file, however, it looks like the module
> > then has another namespace, which means it can't access some variables.
> >
> > Is there a way to simply include the raw text and insert it into your
> > own code, as if it was not in a another file?
> >
> > Thanks for any help,
> >
> > john
> >
> >
> >
> > --
> > nielsenjf at my-Deja.com
> >
> >
> > Sent via Deja.com http://www.deja.com/
> > Before you buy.
> > --
> > http://www.python.org/mailman/listinfo/python-list
> 
> --
> http://www.python.org/mailman/listinfo/python-list

-- 
import string,sys;t=string;f=t.index;j=t.join;print(lambda
s:j(map(lambda
x,s=s:x[:f(x,':')+2]+j(map(lambda y:'%02X'%ord(y),x[f(x,':')+2:]),'
'),map
(lambda i,s=s:("%04X: "%
i)+s[i:i+16],range(0,len(s),16))),'\n'))(open(sys.
argv[1]).read())




More information about the Python-list mailing list