Thomas
Thomas

Reputation: 6752

Is there any possible point to reloading a Python module immediately?

Is there any conceivable point to reloading these modules immediately after importing them? This is the code that I was reviewing which made me wonder:

import time
import sys
import os
import string
import pp
import numpy
import nrrd
reload(nrrd)
import smooth as sm
reload(sm)
import TensorEval2C as tensPP
reload(tensPP)
import TrackFiber4C as trackPP
reload(trackPP)
import cmpV
reload(cmpV)
import vectors as vects
reload(vects)

Edit: I suggested that this might make the creation of .pyc files more likely, but several people pointed out that this happens this first time, every time.

Upvotes: 4

Views: 301

Answers (4)

Katriel
Katriel

Reputation: 123632

It is possible that this does cause something to happen; the obvious example is side-effects that happen on import. For instance, a module could log to a file the time and date of every time it is imported.

There is probably no good reason for this, however.

Upvotes: 6

Gareth Rees
Gareth Rees

Reputation: 65854

I note that the standard modules are just imported: it's the other modules that are reloaded. I expect whoever wrote this code wanted to be able to easily reload the whole package (so as to get their latest edits). After putting in all these redundant reload calls, the programmer only had to write

>>> reload(package)

to bring things up to date in the interpreter, instead of having to type

>>> reload(package.nrrd)
>>> reload(package.sm)
>>> reload(package.tensPP)

etc. So please ignore the suggestion that you commit violence against the programmer who wrote this: they are far from the only programmer who's had trouble with reloading of dependencies. Just encourage them to move the reloads to a convenience function.

Upvotes: 6

Russell Borogove
Russell Borogove

Reputation: 19037

What's the execution environment for this code? There exists at least one Python web framework that makes different reload decisions than standard python does, which leads to frustration and confusion when you make a change that doesn't 'take'.

Upvotes: 0

spiffyman
spiffyman

Reputation: 614

The .pyc files would be created on the first import, so even that's not a very good reason for this.

Upvotes: 1

Related Questions