embedding python in python

Discussion in 'Python' started by Maurice LING, Sep 29, 2004.

  1. Maurice LING

    Maurice LING Guest

    Hi,

    anyone had any experiences in embedding python in python?

    I've tried to do this but it doesn't work.

    eval("from Tkinter import *")

    Thanks
    maurice
    Maurice LING, Sep 29, 2004
    #1
    1. Advertising

  2. Maurice LING

    Phil Frost Guest

    Use exec.

    On Wed, Sep 29, 2004 at 09:23:28AM +0000, Maurice LING wrote:
    > Hi,
    >
    > anyone had any experiences in embedding python in python?
    >
    > I've tried to do this but it doesn't work.
    >
    > eval("from Tkinter import *")
    Phil Frost, Sep 29, 2004
    #2
    1. Advertising

  3. Maurice LING <> wrote in message news:<415a7f0b$>...
    > Hi,
    >
    > anyone had any experiences in embedding python in python?
    >
    > I've tried to do this but it doesn't work.
    >
    > eval("from Tkinter import *")
    >
    > Thanks
    > maurice


    You need exec for statements:

    exec("from Tkinter import *")
    Lonnie Princehouse, Sep 29, 2004
    #3
  4. Maurice LING

    Maurice LING Guest

    Lonnie Princehouse wrote:
    > Maurice LING <> wrote in message news:<415a7f0b$>...
    >
    >>Hi,
    >>
    >>anyone had any experiences in embedding python in python?
    >>
    >>I've tried to do this but it doesn't work.
    >>
    >>eval("from Tkinter import *")
    >>
    >>Thanks
    >>maurice

    >
    >
    > You need exec for statements:
    >
    > exec("from Tkinter import *")


    Thank you.
    Maurice LING, Sep 30, 2004
    #4
  5. Maurice LING

    Steve Holden Guest

    Maurice LING wrote:

    > Lonnie Princehouse wrote:
    >
    >> Maurice LING <> wrote in message
    >> news:<415a7f0b$>...
    >>
    >>> Hi,
    >>>
    >>> anyone had any experiences in embedding python in python?
    >>>
    >>> I've tried to do this but it doesn't work.
    >>>
    >>> eval("from Tkinter import *")
    >>>
    >>> Thanks
    >>> maurice

    >>
    >>
    >>
    >> You need exec for statements:
    >>
    >> exec("from Tkinter import *")

    >
    >
    > Thank you.

    .... and note that exec is a statement introduced by a keyword, not a
    function call, so

    exec "from Tkinter import *"

    is a less misleading way to write it.

    regards
    Steve
    Steve Holden, Sep 30, 2004
    #5
  6. Maurice LING

    Maurice LING Guest

    Hi,

    Sorry, I have another problem here. Given this snipplet,

    >>> def b(s):

    .... exec(s)
    .... exec('print "x= " + str(x)')
    ....
    >>> b('x = 10')

    x= 10
    >>>
    >>> print x

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'x' is not defined
    >>>


    Clearly, all the objects that were declared using exec() in function b
    went out of scope, is there anyway to prevent this from happening?

    What I really need is this, for example, in a class,

    1. a function to initialize a set of objects
    2. a function which carries commands to act on the object (unknown at
    compile time)
    3. a function to read the values of the set of objects

    So, if the objects went out of scope after leaving (1), then I am rather
    screwed when I'm in (2). Any remedies?

    Thanks
    Maurice
    Maurice LING, Oct 1, 2004
    #6
  7. Maurice LING

    Jeff Shannon Guest

    Maurice LING wrote:

    > Hi,
    >
    > Sorry, I have another problem here. Given this snipplet,
    >
    > >>> def b(s):

    > ... exec(s)
    > ... exec('print "x= " + str(x)')
    > ...
    > >>> b('x = 10')

    > x= 10
    > >>>
    > >>> print x

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > NameError: name 'x' is not defined
    > >>>

    >
    > Clearly, all the objects that were declared using exec() in function b
    > went out of scope, is there anyway to prevent this from happening?



    >>> myglobals = {}
    >>> mylocals = {}
    >>> exec "b = 1 + 2" in myglobals, mylocals
    >>> mylocals

    {'b': 3}
    >>> exec "c = b * 2" in myglobals, mylocals
    >>> mylocals

    {'c': 6, 'b': 3}
    >>> >>>


    The exec statement can take an optional pair of dictionaries that it
    uses as the global and local namespaces to execute the given code in.
    This is *much* preferable to a bare exec for several reasons, one of
    them being exactly your issue, and another being safety -- since you
    must explicitly include any names that you want exec to use, it makes
    exec rather less likely to unintentionally stomp all over your namespace
    (or for malicious code to do certain types of harm).

    Jeff Shannon
    Technician/Programmer
    Credit International
    Jeff Shannon, Oct 1, 2004
    #7
  8. Maurice LING

    Maurice LING Guest


    >
    > >>> myglobals = {}
    > >>> mylocals = {}
    > >>> exec "b = 1 + 2" in myglobals, mylocals
    > >>> mylocals

    > {'b': 3}
    > >>> exec "c = b * 2" in myglobals, mylocals
    > >>> mylocals

    > {'c': 6, 'b': 3}
    > >>> >>>

    >
    > The exec statement can take an optional pair of dictionaries that it
    > uses as the global and local namespaces to execute the given code in.
    > This is *much* preferable to a bare exec for several reasons, one of
    > them being exactly your issue, and another being safety -- since you
    > must explicitly include any names that you want exec to use, it makes
    > exec rather less likely to unintentionally stomp all over your namespace
    > (or for malicious code to do certain types of harm).
    >
    > Jeff Shannon
    > Technician/Programmer
    > Credit International
    >


    Please pardon me for my dumbness but what exactly goes on in


    exec "b = 1 + 2" in myglobals, mylocals

    ?

    Thanks
    Maurice
    Maurice LING, Oct 1, 2004
    #8
  9. Maurice LING

    Jeff Shannon Guest

    Maurice LING wrote:

    > Please pardon me for my dumbness but what exactly goes on in
    >
    > exec "b = 1 + 2" in myglobals, mylocals



    What's happening is that I've provided two dictionaries for exec to use
    as a global namespace and a local namespace, respectively. In this
    particular example, both namespaces are empty, but we're not looking up
    any names in those namespaces, so that doesn't matter. We *are* binding
    a name ('b') in the local namespace. That local namespace is the dict
    I've named mylocals, so when exec is finished I can reference
    mylocals['b'] and find the value that 'b' was bound to inside the exec
    statement.

    The next example is a bit more complex. I'm passing the same
    dictionaries, but now the local namespace contains that reference to
    'b'. The exec statement looks up that name, finds that it's bound to
    the value 3, multiplies that value by 2, and binds the result to 'c',
    again all in the local namespace (which is still the mylocals dict).
    Now I've got *two* names bound in mylocals -- 'b' and 'c'.

    Note that my initial dictionaries don't need to be empty. Just as was
    demonstrated in the second example, if there are entries in those
    dictionaries then the exec'd code can refer to them as if they were
    normal (global or local) variable names. This also means that exec'd
    code can't refer to any *other* global or local variables --

    >>> myglobals = {}
    >>> mylocals = {}
    >>> foo = 5
    >>> exec "bar = foo * 5"
    >>> bar

    25
    >>> exec "bar = foo * 5" in myglobals, mylocals

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    File "<string>", line 1, in ?
    NameError: name 'foo' is not defined
    >>> myglobals['foo'] = 10
    >>> exec "bar = foo * 5" in myglobals, mylocals
    >>> mylocals

    {'bar': 50}
    >>>


    When using a bare exec, the current local and global namespaces are
    used, so exec finds 'foo' and binds 'bar'. But when I give exec two
    empty dicts, the name 'foo' is undefined within the exec statement, even
    though it exists in the interpreter's namespace. After creating 'foo'
    within myglobals, though, exec can find it, and then binds 'bar' within
    mylocals.

    In short, you can carefully construct a global and local namespace for
    your exec'd code, which contains only those variables that you *want*
    that code to see. If that exec'd code binds any new variable names,
    then those names will remain in those namespaces when exec is done, so
    you can retrieve them later.

    Jeff Shannon
    Technician/Programmer
    Credit International
    Jeff Shannon, Oct 1, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darryl

    Embedding Python in Python

    Darryl, Oct 8, 2003, in forum: Python
    Replies:
    6
    Views:
    398
    Darryl
    Oct 14, 2003
  2. Phil Frost

    Embedding Python in Python

    Phil Frost, Aug 18, 2004, in forum: Python
    Replies:
    23
    Views:
    770
    Paul Rubin
    Aug 19, 2004
  3. Kakacek
    Replies:
    1
    Views:
    379
    Kakacek
    Jan 12, 2006
  4. abhinav
    Replies:
    1
    Views:
    438
    Armin Steinhoff
    Feb 19, 2006
  5. Shankar
    Replies:
    1
    Views:
    705
    Serge Orlov
    May 5, 2006
Loading...

Share This Page