0

I read a lot on reload but I am not able to use reload function.There is some error in imp.py itself.I didn't make any changes.

>>> import imp
>>> imp.reload('fileread')
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    imp.reload('fileread')
  File "C:\Python33\lib\imp.py", line 258, in reload
    raise TypeError("reload() argument must be module")
TypeError: reload() argument must be module

fileread is stored in proper directory of python.

1 Answer 1

3

You need to pass actual module objects to imp.reload().

If you only have the module name, look up the module object in the sys.modules mapping:

import sys
import imp

imp.reload(sys.modules['fileread'])

This only works on modules that have already been imported; if some of your entries are not imported yet, at the very least catch the KeyError skip these:

try:
    imp.reload(sys.modules[modulename])
except KeyError:
    # not loaded, no point in reloading
    pass

Optionally, you could use importlib.import_module() to load such modules instead.

Sign up to request clarification or add additional context in comments.

2 Comments

>>> import sys >>> import imp >>> imp.reload(sys.modules['fileread']) Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> imp.reload(sys.modules['fileread']) KeyError: 'fileread' I am still getting error
@PavanRavishankar: Then the module is not imported. Import fileread first; at that point there is no need to reload either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.