Is there a sessionInfo() equivalent in Python?

ipython, python, r

Solution

Update 2019-03-14

I ended up building a package for this myself called `session_info` to have more flexibility in the output (e.g. show modules imported indirectly through other modules) and to have access to this functionality outside notebooks. It can be installed via `pip install session-info` and used like so:

# Some sample imported modules
import math

import natsort
import pandas
import session_info


session_info.show()

which gives you output similar to

Session information:
-----
natsort             7.1.1
pandas              1.2.2
session_info        1.0.0
-----
IPython             7.23.0
jupyter_client      6.1.12
jupyter_core        4.7.1
-----
Python 3.9.2 | packaged by conda-forge | (default, Feb 21 2021, 05:02:46) [GCC 9.3.0]
Linux-5.11.13-arch1-1-x86_64-with-glibc2.33
-----
Session information updated at 2021-05-06 09:59

Original answer

There is a magic package called version_information that accomplishes this. Install with `pip install version_information`. (Note this extension hasn't been updated in a while, there is a more recent one called watermark)

%load_ext version_information
%version_information pandas, numpy, seaborn

Output:

You could also accomplish something similar using the solution from How to list imported modules? together with `!pip freeze`.

#find the names of the imported modules
import types
def imports():
    for name, val in globals().items():
        if isinstance(val, types.ModuleType):
            yield val.__name__

#exclude all modules not listed by `!pip freeze`
excludes = ['__builtin__', 'types', 'IPython.core.shadowns', 'sys', 'os']
imported_modules = [module for module in imports() if module not in excludes]
pip_modules = !pip freeze #you could also use `!conda list` with anaconda

#print the names and versions of the imported modules
for module in pip_modules:
    name, version = module.split('==')
    if name in imported_modules:
        print(name + '\t' + version)

Output:

pandas  0.16.2
numpy   1.9.2
seaborn 0.5.1

I couldn't figure out how to pass a list with the imported modules (e.g. `modulenames`) to the `%version_information` magic command (all quotation marks need to be removed), so maybe someone can improve this answer by adding that info.

Problem

Normally I use R, and often when wanting to make things reproduicible I use `sessionInfo()`. The reason for this is that I like to let people know what version of everything I am using and what packages I have installed/loaded and what OS I am on etc, so that its quite clear. `sessionInfo` returns the version of R, the processor type (e.g. 32/64 bit x86), the operating system, the locale details, and which packages have been loaded. I am new to python and wondered if there is an equivalent for Python? I'm hoping to use it in an iPython Notebook...

Original source

Related problems