472,374 Members | 1,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Importing/reloading modules

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 without
having to shut down and restart the server.

Now, what I want to do at the top of my main module, instead of
just "import somemodule", is something like this:

try:
reload(somemodule)
except NameError:
import somemodule

. . . so that it imports the module if it's just starting up, and
reloads it otherwise. The problem is that I want to be able to call
this code multiple times, so I can call it from the "reload" command.
I can't figure out how to do this appropriately.

If I put the try/except in a function, it doesn't import the
module name into the global namespace, so that's no good. I could, of
course, import the modules normally and write a separate function that
reloads them, but then if I decided to import another module or remove
one, I'd have to change both pieces of code.

What I want is to get one piece of code that imports modules, or
reloads them if they're already imported, and I need to be able to
call this code like a function, from anywhere in my program. (There
are also "from foo import bar" statements that I need to re-execute
along with the reloads.) Something like:

importFunc():
from foo import bar # except import it into the global namespace
try:
reload(baz)
except NameError:
import baz # except import it into the global namespace

Someone suggested that I fiddle with __import__, but I'm not sure
exactly what to do, or if this is the best approach. The
documentation seems to suggest that simply doing __import__
('somemodule', globals(), globals()) won't actually stick the module
name into the global namespace -- is there some way to make this
happen, though?

--
--OKB (not okblacke)
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
Jul 18 '05 #1
0 1251

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

Similar topics

2
by: Andy Jewell | last post by:
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'...
4
by: david farning | last post by:
I am having a problem with name space contention. I am combining two existing programs both with their own config modules. I have been expermenting with import config #grab first config...
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...
12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
6
by: Kamilche | last post by:
I have a large project that is getting complex, and I would like to print the docstrings without importing the modules. The only Python utility I could find references is apparently defunct and...
14
by: T. Crane | last post by:
Hi, When troubleshooting code that's saved in a text file, I often find that I want to make a change to it, re-save it, then reimport it. However, just typing import myTestCode doesn't...
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: Martin P. Hellwig | last post by:
Hello all, I had some troubles in the past how to arrange my packages and modules, because I usually don't develop my stuff in the Lib\site-packages directory I have some troubles when importing...
7
by: Hussein B | last post by:
Hey, Suppose I have a Python application consists of many modules (lets say it is a Django application). If all the modules files are importing sys module, how many times the sys module will be...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.