Skip to content Skip to sidebar Skip to footer

Get Description Of An Installed Package Without Actual Importing It

If you type this: import somemodule help(somemodule) it will print out paged package description. I would need to get the same description as a string but without importing this p

Solution 1:

The reason that you will need to import the module to get a help string is that in many cases, the help strings are actually generated in code. It would be pointlessly difficult to parse the text of such a package to get the string since you would then have to write a small Python interpreter to reconstruct the actual string.

That being said, there are ways of completely deleting a temporarily imported modules based on this answer, which summarizes a thread that appeared on the Python mailing list around 2003: http://web.archive.org/web/20080926094551/http://mail.python.org/pipermail/python-list/2003-December/241654.html. The methods described here will generally only work if the module is not referenced elsewhere. Otherwise the module will be unloaded in the sense that import will reload it from scratch instead of using the existing sys.modules entry, but the module will still live in memory.

Here is a function that does approximately what you want and even prints a warning if the module does not appear to have been unloaded. Unlike the solutions proposed in the linked answer, this function really handles all the side-effects of loading a module, including the fact that importing one package may import other external packages into sys.modules:

import sys, warnings
defget_help(module_name):
    modules_copy = sys.modules.copy()
    module = __import__(module_name)
    h = help(module)
    for modname inlist(sys.modules):
        if modname notin modules_copy:
            del sys[modname]
    if sys.getrefcount(module) > 1:
        warnings.warn('Module {} is likely not to be completely wiped'.format(module_name))
    del module
    return h

The reason that I make a list of the keys in the final loop is that it is inadvisable to modify a dictionary (or any other iterable) as you iterate through it. At least in Python 3, dict.keys() returns an iterable that is backed by the dictionary itself, not a frozen copy. I am not sure if h = ... and return h are even necessary, but in the worst case, h is just None.

Solution 2:

Well, if you are only worried about keeping the global namespace tidy, you could always import in a function:

>>>defget_help():...import math...help(math)...>>>math
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined

Solution 3:

I would suggest a different approach, if i understand you correctly, you wish to read a portion of a package, without importing it (even within a function with local scope). I would suggest a method to do so would be via accessing the (python_path)/Lib/site-packages/(package_name)/ and reading the contents of the respective files as an alternative to importing the module so Python can.

Post a Comment for "Get Description Of An Installed Package Without Actual Importing It"