473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reloading nested modules

Does anyone know of a way to dynamically reload all the imported modules of a
client module?

I'm writing a program that I have broken down into quite a few submodules, and
the 'configuration' is done with modules too. As I am developing the app, I
need to test bits and pieces here and there. This is what I currently do:

In each module, I have a section, just after the main imports like so:
-----------8<-----------
# normal imports:

from ooby import squibble,dibble
import dooby
import doo

# reloads for debugging

import ooby # create a reference to ooby so we can reload it
reload(ooby)
reload(dooby)
reload(doo)

# main module code begins...
-----------8<-----------
The debugging imports of course will be dropped when the program is stable.

I also often find myself doing similar stuff from the command-line, like:

reload(ooby.oojar); reload(ooby.dibble); reload(ooby); ooby.somefunc(.....)

I just wondered if anyone had develped a better 'spell' or even a small script
that uses some clever intorspecton hack, before I go and start trying to
reinvent the wheel...

-andyj

Jul 18 '05 #1
2 2450
I've wondered about the same problem and considered that as a solution, but
never tested it. I assume that *wouldn't* rebind all of the imports that
have already happened.. ie, I think the references to the old copies of
the modules would hang around even though new ones have been imported.

Regardless, the scoping doesn't work so an attempt to reload the embedded
module assumes it is available in local scope and fails. Again, that would
lead me to believe my first statement is true, but when I tested just now,
I got no further than the scoping problem...

I've been intended to write something that will take a module name and
rebind it in all namespaces that have it currently, but haven't got around
to it. If I ever do, I'll post it here :)

Greg

Martin v. Löwis wrote:
Andy Jewell <an**@wild-flower.co.uk> writes:
Does anyone know of a way to dynamically reload all the imported
modules of a client module?


You could iterate over sys.modules and invoke reload for all of them.

Regards,
Martin


Jul 18 '05 #2
Greg Fortune <li***@gregfortune.com> writes:
I've wondered about the same problem and considered that as a solution, but
never tested it. I assume that *wouldn't* rebind all of the imports that
have already happened.. ie, I think the references to the old copies of
the modules would hang around even though new ones have been imported.
Depends on the import. For

import foo

the reload would take effect, as, on reload, the module object stays,
its dictionary stays, and it is just the dictionary contents that is
recreated.

For

from foo import bar

you still have the old value of bar after reloading.
Regardless, the scoping doesn't work so an attempt to reload the embedded
module assumes it is available in local scope and fails. Again, that would
lead me to believe my first statement is true, but when I tested just now,
I got no further than the scoping problem...
What scoping problem?
I've been intended to write something that will take a module name and
rebind it in all namespaces that have it currently, but haven't got around
to it. If I ever do, I'll post it here :)


Just ask Guido to borrow you the time machine - this has already been
done.

Regards,
Martin

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: OKB (not okblacke) | last post by:
I'm fooling around with some MUD server code, and I want to add a "reload" command that will let MUD wizards reload the server modules, so that changes to the MUD parser and such can be effected...
2
by: aurora | last post by:
I am looking for a way for reloading updated modules in a long running server. I'm not too concerned about cascaded reload or objects already created. Just need to reload module xxx if the...
2
by: Lowell Kirsh | last post by:
I have a driver module as well as several other modules. I am running the driver interactively from emacs - that is, I don't restart python on each run. I want to work it such that every time a...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
3
by: Seymour | last post by:
I created a module with the DictAdder subclass as follows: class DictAdder(Adder): def add(self, x, y): new={} for k in x.keys(): new=x for k in y.keys(): new=y return new At the...
4
by: aine_canby | last post by:
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...
1
by: tsuraan | last post by:
Supposing that I have a directory tree like so: a/ __init__.py b/ __init__.py c.py and b.py has some method (let's call it d) within it. I can, from python, do:
1
by: lukasz.f24 | last post by:
Hello, I came across annoying problem during my fun with mod_python. I turned out that mod_python load package only onca and don't care about any changes to it. Obviously it makes sense on...
7
by: Tlis | last post by:
I am using a software system with an embedded Python interpreter (version 2.3) for scripting. The KcsPoint2D.py module contains a Point2D class with the following method: def...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.