Namespace problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kiran

    #1

    Namespace problems

    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

  • Kiran

    #2
    Re: Namespace problems

    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

    Comment

    • Kay Schluehr

      #3
      Re: Namespace problems

      Kiran wrote:[color=blue]
      > 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[/color]

      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:
      [color=blue][color=green][color=darkred]
      >>> import PyShell
      >>> PyShell.main() # causes creation of main window for PyShell[/color][/color][/color]

      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

      Comment

      Working...