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

  • Thread starter John Perks and Sarah Mount
  • Start date
J

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.

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
 
P

Paul Clinch

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top