dynamic loading of code, and avoiding package/module name collisions.

Discussion in 'Python' started by John Perks and Sarah Mount, Apr 7, 2005.

  1. Long story short: what I'm looking for is information on how have a Python
    app that:
    * embeds an editor (or wxNoteBook full of editors)
    * loads code from the editors' text pane into the app
    * executes bits of it
    * then later unloads to make way for an edited version of the code.
    The new version needs to operate on a blank slate, so reload() may not be
    appropriate.

    One of the reasons I am asking is that I am writing a simulator, where the
    behaviour of the objects is written in Python by the user (with callbacks
    into the simulator's API to update the display). How should I best go about
    this, with respect to loading (and unloading) of user code? It is expected
    that each project will take the form of a Python package, with some files
    therein having standard names for particular purposes (presumably
    __init__.py will be one of these), and the rest being user code and data
    files. It is not expected that we restrict the user's package hierarchy to
    be only one level deep. Given the name of a package:

    How can we be sure it doesn't conflict with a Python package/module name?

    What if a new module/package is added (e.g. to site-packages) that has the
    same name?

    One possibility that occurred to me was having a package
    MyAppName.UserProject as part of the app, with the user's project root
    package occurring as a subpackage in there, and being reloaded (either via
    reload() or a method on imp) each time we re-start the simulation. One
    reason for this was to avoid top-level name collisions. Is this a good way
    to go about it, and if so, how should users' imports refer between modules
    in the same package, and across sub-packages?

    Can someone please explain the interaction between the relevant parts of the
    imp module (which is presumably what I'll be needing) and: the import lock;
    sys.modules; sys.path?

    Is removing a module from sys.modules tantamount to uninstalling it? In
    particular, will imp.load_module() then create a new one?

    When cheking the user's intended filenames for validity as module names, is
    the canonical regexp to use
    [a-zA-Z_][a-zA-Z0-9_]*, or is it something more subtle?

    In general, on deciding on a module file name, how can one be sure that it
    doesn't conflict with another package (either standard or third-party)? In
    particular, if I have a thing.py file in my local dir, and then Python 2.5
    brings out a (possibly undocumented) standard module called thing, how can I
    avoid problems with running python from my local dir? (As an experiment, I
    put a file called ntpath.py in my local dir, and I couldn't import site.) To
    make matters worse, I may be unaware of this Python upgrade if it is done by
    the sysadmin. The same applies with new keywords in Python, but this may be
    ameliorated by their gradual introduction via __future__.

    If all this wasn't complicated enough, at some point we'll want to let the
    users write their code in Java as well (though not mixing langauges in one
    project; that would make my brain hurt). Can someone point to a resource
    that will list the issues we should be aware of from the start? (Googling
    just gets references to Jython and jPype, rather than anything than, say, a
    list of gotchas).

    Thank you

    John
     
    John Perks and Sarah Mount, Apr 7, 2005
    #1
    1. Advertising

  2. John Perks and Sarah Mount

    Paul Clinch Guest

    Dear "John Perks and Sarah Mount",

    > Long story short: what I'm looking for is information on how have a Python
    > app that:
    > * embeds an editor (or wxNoteBook full of editors)
    > * loads code from the editors' text pane into the app
    > * executes bits of it
    > * then later unloads to make way for an edited version of the code.
    > The new version needs to operate on a blank slate, so reload() may not be
    > appropriate.



    This sounds like 'idle'.

    Regards, Paul C.
     
    Paul Clinch, Apr 8, 2005
    #2
    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. Vam
    Replies:
    2
    Views:
    306
    Andrey Tarasevich
    Feb 14, 2005
  2. Richard Bos
    Replies:
    3
    Views:
    367
    CBFalconer
    Feb 6, 2004
  3. Peter Ammon

    Avoiding name collisions in macros

    Peter Ammon, May 21, 2004, in forum: C Programming
    Replies:
    6
    Views:
    597
    Chris Torek
    May 23, 2004
  4. George P
    Replies:
    3
    Views:
    695
    Alex Martelli
    Sep 11, 2004
  5. Robert Brewer
    Replies:
    0
    Views:
    247
    Robert Brewer
    Oct 6, 2004
Loading...

Share This Page