如何在Python3中列出导入的模块和版本 [英] How to list imported modules and version in Python3

查看:140
本文介绍了如何在Python3中列出导入的模块和版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有几个关于SO的问题似乎可以解决此问题(例如).但是,报告的解决方案似乎在我的代码(python 3.6.5)中不起作用.在第一种情况下,打印的模块较少,在第二种情况下,显示的信息更多(sys.modules.keys()).

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()).

是否有一种简便的方法,可以以与sessionInfo()类似的方式在__version__)(通过__version__) "rel =" nofollow noreferrer> R ?

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

推荐答案

这对我有用:

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

这篇关于如何在Python3中列出导入的模块和版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆