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.