Namespace problems

Kay Schluehr kay.schluehr at gmx.net
Tue Jun 6 11:27:29 EDT 2006


Kiran wrote:
> Hello All,
>   I am kind of a beginner to python, but here is the deal.
>
>   I am writing a wxpython Application, in which I have a GUI.  This GUI
> imports some other modules that I have written, using the format
> import mymodule as _MyModule
>
>   Now, this GUI in addition to having all it's bells and whistles has a
> console (from pyshell), so that the user can run their own scripts
> inside this GUI.
>   What I am trying to do is to get these scripts that the user runs to
> be able to see all the other objects created and modules imported by
> the GUI.
>
>   To make this clear, what I want to be able to do for example is in my
> script, do ,
>          dir(mymodule),
>   already having imported mymodule in the GUI.
>   Any ideas on how to make this happen?  I don't want to import
> mymodule in this script, i want this script to see the mymodule that I
> imported in the main GUI.
>
> thanks a lot for your help,
> Kiran

Well, it depends on the context. If the __main__ context is that of
PyShell it will likely contain no GUI object and you shall pass the GUI
to PyShell before a PyShell window is created ( e.g.
PyShell.__dict__["GUI"] = GUI ). Descendent objects like mymodule that
live in the GUI context will be accessible in the usual way i.e. by
GUI.mymodule.

You can experiment a little bit with contexts. Simply open a Python
session and import PyShell:

>>> import PyShell
>>> PyShell.main()   # causes creation of main window for PyShell

Now you can inspect locals of PyShell using dir(). In this case the
__main__ context is that of the top level session in which PyShell runs
as an own session. You just have to import GUI to make it accessible to
PyShell. If PyShell is the __main__ context you might assign GUI as
described above.

Regards,
Kay




More information about the Python-list mailing list