How to list imported modules and version in Python3 [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-11 23:18:31

问题


I am aware that several questions on SO seem to address this problem (e.g. this, this). However, the reported solution seem not to work in my code (python 3.6.5). In the first case, fewer modules are printed, in the second case a lot more information is displayed (sys.modules.keys()).

Is there an easy way to display all imported top modules together with versions (through __version__) in a similar fashion as sessionInfo() in R?


回答1:


This worked for me:

import sys
for module in sys.modules:
    try:
        print(module,sys.modules[module].__version__)
    except:
        try:
            if  type(modules[module].version) is str:
                print(module,sys.modules[module].version)
            else:
                print(module,sys.modules[module].version())
        except:
            try:
                print(module,sys.modules[module].VERSION)
            except:
                pass


来源:https://stackoverflow.com/questions/50395556/how-to-list-imported-modules-and-version-in-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!