1

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?

gc5
  • 9,468
  • 24
  • 90
  • 151
  • 1
    The first answer you linked to worked for me (Python v3.6.1). What exactly didn't work for you with the code in the first answer? What does _". . . fewer modules are printed . . ."_ mean? – Christian Dean May 17 '18 at 15:47
  • @ChristianDean I imported at least 8 modules, but only 2 are shown when I run the code of the first answer – gc5 May 17 '18 at 15:52
  • @ChristianDean my problem was with aliases, adapting this solution to Python3 works: https://stackoverflow.com/a/38268153/41977 – gc5 May 17 '18 at 16:27
  • Okay @gc5, since the solution on that question worked for you, I went ahead an dupe-hammered your question. – Christian Dean May 17 '18 at 16:35

1 Answers1

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
Paula Thomas
  • 1,152
  • 1
  • 8
  • 13
  • To add to this solution, if you want to omit builtins I suggest looking at this answer (to adapt for Python3): https://stackoverflow.com/a/38268153/41977 – gc5 May 17 '18 at 16:31
  • 2
    I put together a very simple package to do this using your suggestion: https://github.com/fbrundu/py_session – gc5 May 17 '18 at 19:59
  • This didn't work at all. Only lists a few internal modules with a version string attached. – not2qubit Jan 14 '22 at 06:43