Python посмотреть импортированные модули

modulefinder — Find modules used by a script¶

This module provides a ModuleFinder class that can be used to determine the set of modules imported by a script. modulefinder.py can also be run as a script, giving the filename of a Python script as its argument, after which a report of the imported modules will be printed.

modulefinder. AddPackagePath ( pkg_name , path ) ¶

Record that the package named pkg_name can be found in the specified path.

modulefinder. ReplacePackage ( oldname , newname ) ¶

Allows specifying that the module named oldname is in fact the package named newname.

class modulefinder. ModuleFinder ( path = None , debug = 0 , excludes = [] , replace_paths = [] ) ¶

This class provides run_script() and report() methods to determine the set of modules imported by a script. path can be a list of directories to search for modules; if not specified, sys.path is used. debug sets the debugging level; higher values make the class print debugging messages about what it’s doing. excludes is a list of module names to exclude from the analysis. replace_paths is a list of (oldpath, newpath) tuples that will be replaced in module paths.

Print a report to standard output that lists the modules imported by the script and their paths, as well as modules that are missing or seem to be missing.

Analyze the contents of the pathname file, which must contain Python code.

A dictionary mapping module names to modules. See Example usage of ModuleFinder .

Example usage of ModuleFinder ¶

The script that is going to get analyzed later on (bacon.py):

import re, itertools try: import baconhameggs except ImportError: pass try: import guido.python.ham except ImportError: pass 

The script that will output the report of bacon.py:

from modulefinder import ModuleFinder finder = ModuleFinder() finder.run_script('bacon.py') print('Loaded modules:') for name, mod in finder.modules.items(): print('%s: ' % name, end='') print(','.join(list(mod.globalnames.keys())[:3])) print('-'*50) print('Modules not imported:') print('\n'.join(finder.badmodules.keys())) 

Sample output (may vary depending on the architecture):

Loaded modules: _types: copyreg: _inverted_registry,_slotnames,__all__ re._compiler: isstring,_sre,_optimize_unicode _sre: re._constants: REPEAT_ONE,makedict,AT_END_LINE sys: re: __module__,finditer,_expand itertools: __main__: re,itertools,baconhameggs re._parser: _PATTERNENDERS,SRE_FLAG_UNICODE array: types: __module__,IntType,TypeType --------------------------------------------------- Modules not imported: guido.python.ham baconhameggs 

Источник

Читайте также:  Чудеса генетики питон vore

How to get a list of modules imported by a python module

I want to access some kind of import dependency tracking in Python, if there is any. I decided to add to my module a __dependencies__ dict describing versions of all modules imported by the module. I want to have an automated way of obtaining a list of modules imported by my module. Preferably in the last line of the module. ModuleFinder (as suggested by How to list imports within a Python module? ) would not work since the inspection shall be performed for already loaded module. Another problem with ModuleFinder is that it inspects a Python script (with if __name__ == ‘__main__’ branch), not a module. If we consider a toy script script.py:

if __name__ == '__main__': import foo 
>>> mf = ModuleFinder >>> mf.run_script('script.py') >>> 'foo' in mf.modules True 

which should be False if script is imported as a module. I do not want to list all imported modules — only modules imported by my module — so sys.modules (suggested by What is the best way of listing all imported modules in python?) would return too much. I may compare snapshots of sys.modules from the beginning and the end of the module code. But that way I would miss all modules used by my module, but imported before by any other module. It is important to list also modules from which the module imports objects. If we consider a toy module example.py:

from foo import bar import baz 
>>> import example >>> moduleImports(example) , 'baz': > 

(it may contain also modules imported recursively or foo.bar given bar is a module). Use of globls() (according to How to list imported modules?) requires me to deal with non-module imports manually, like:

from foo import bar import bar 

How can I avoid that? There is another issue with my solution so far. PyCharm tends to clean up my manual imports on refactoring, which makes it hard to keep it working.

Читайте также:  Есть ли java на nokia

Источник

Оцените статью