Namespace problems

K

Kiran

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
 
K

Kiran

I should note that if you use the execfile command in the console, the
script runs, but if you import the script, it says it cant find the
module
 
K

Kay Schluehr

Kiran said:
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:

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
 

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

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,332
Latest member
LeesaButts

Latest Threads

Top