reloading modules

Discussion in 'Python' started by aine_canby@yahoo.com, Dec 6, 2006.

  1. Guest

    I'm using python.exe to execute my modules. I have a music.py module
    which contains my classes and a main.py module which uses these
    classes. In python.exe, I call "import main" to execute my program. The
    problem is that I have to close python and reopen it everytime i change
    music.py or main.py. What should I be doing.

    Thanks,

    Aine.
    , Dec 6, 2006
    #1
    1. Advertising

  2. Dustan Guest

    wrote:
    > I'm using python.exe to execute my modules. I have a music.py module
    > which contains my classes and a main.py module which uses these
    > classes. In python.exe, I call "import main" to execute my program. The
    > problem is that I have to close python and reopen it everytime i change
    > music.py or main.py. What should I be doing.
    >
    > Thanks,
    >
    > Aine.


    >>> import main

    ### Execution Occurs ###
    >>> # You go off to edit your module
    >>> reload(main)

    ### Execution Occurs ###
    Dustan, Dec 6, 2006
    #2
    1. Advertising

  3. Dustan Guest

    Dustan wrote:
    > wrote:
    > > I'm using python.exe to execute my modules. I have a music.py module
    > > which contains my classes and a main.py module which uses these
    > > classes. In python.exe, I call "import main" to execute my program. The
    > > problem is that I have to close python and reopen it everytime i change
    > > music.py or main.py. What should I be doing.
    > >
    > > Thanks,
    > >
    > > Aine.

    >
    > >>> import main

    > ### Execution Occurs ###
    > >>> # You go off to edit your module
    > >>> reload(main)

    > ### Execution Occurs ###


    I was obviously assuming that your module does something just by
    importing, which may or may not be the case. Either way, whenever a
    module may have been edited during a program's lifetime, it can be
    reloaded using the reload function.
    Dustan, Dec 6, 2006
    #3
  4. wrote:
    > I'm using python.exe to execute my modules. I have a music.py module
    > which contains my classes and a main.py module which uses these
    > classes. In python.exe, I call "import main" to execute my program. The
    > problem is that I have to close python and reopen it everytime i change
    > music.py or main.py. What should I be doing.
    >
    > Thanks,
    >
    > Aine.


    Instead of calling 'import', use the 'imp' module and call
    'find_module' followed by 'load_module'. As the docs explain, one of
    the differences between this and 'import' is that it loads the module
    every time, even if it is already imported.

    HTH

    Frank Millman
    Frank Millman, Dec 6, 2006
    #4
  5. Kent Johnson Guest

    Dustan wrote:
    > wrote:
    >> I'm using python.exe to execute my modules. I have a music.py module
    >> which contains my classes and a main.py module which uses these
    >> classes. In python.exe, I call "import main" to execute my program. The
    >> problem is that I have to close python and reopen it everytime i change
    >> music.py or main.py. What should I be doing.
    >>
    >> Thanks,
    >>
    >> Aine.

    >
    >>>> import main

    > ### Execution Occurs ###
    >>>> # You go off to edit your module
    >>>> reload(main)

    > ### Execution Occurs ###


    If you edit music.py you will have to
    import music
    reload(music)

    to get the new music module, then
    reload(main)

    to run again.

    You could just type
    > python main.py


    at the command line each time you want to run, or use an editor that
    lets you run the program from within the editor.

    Kent
    Kent Johnson, Dec 15, 2006
    #5
    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. Andy Jewell

    Reloading nested modules

    Andy Jewell, Jul 13, 2003, in forum: Python
    Replies:
    2
    Views:
    302
    Martin v. =?iso-8859-15?q?L=F6wis?=
    Jul 14, 2003
  2. OKB (not okblacke)

    Importing/reloading modules

    OKB (not okblacke), Sep 4, 2003, in forum: Python
    Replies:
    0
    Views:
    294
    OKB (not okblacke)
    Sep 4, 2003
  3. Joseph Krauze

    reloading modules

    Joseph Krauze, Dec 3, 2003, in forum: Python
    Replies:
    0
    Views:
    288
    Joseph Krauze
    Dec 3, 2003
  4. aurora
    Replies:
    2
    Views:
    286
    Bryan
    Oct 16, 2004
  5. Peter Peyman Puk

    Importing v reloading modules modules

    Peter Peyman Puk, Mar 19, 2010, in forum: Python
    Replies:
    0
    Views:
    297
    Peter Peyman Puk
    Mar 19, 2010
Loading...

Share This Page