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...
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
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.
The following will make you part there :
In [1]: import IPython
In [2]: print IPython.sys_info()
{'codename': 'Work in Progress',
'commit_hash': '4dd36bf',
'commit_source': 'repository',
'default_encoding': 'UTF-8',
'ipython_path': '/Users/matthiasbussonnier/ipython/IPython',
'ipython_version': '2.0.0-dev',
'os_name': 'posix',
'platform': 'Darwin-11.4.2-x86_64-i386-64bit',
'sys_executable': '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python',
'sys_platform': 'darwin',
'sys_version': '2.7.6 (default, Nov 28 2013, 17:25:22) \n[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)]'}
Otherwise there is no standard way to get the version of imported modules. pip freeze
will wive you most of installed version of modules on your machine though:
In [3]: !pip freeze
Cython==0.20dev
Django==1.4.2
Fabric==1.7.0
Flask==0.9
Flask-Cache==0.10.1
Flask-Markdown==0.3
Flask-SQLAlchemy==0.16
Jinja2==2.7.1
Logbook==0.6.0
...
This is something we think should be solved in python before making IPython 'magics' that help with it. This is often requested and we haven't yes find a compromise of what should be done and what would be the requirements.
Conda related answer
As I regularly install and run jupyter-notebooks with conda (instead of pip
), I will leave this here in case it is helpful to someone else. At the end of my notebooks I usually have a cell with:
!conda list
# packages in environment at /path/to/my/conda_env:
#
# Name Version Build Channel
_r-mutex 1.0.1 anacondar_1 conda-forge
appnope 0.1.2 py39h6e9494a_1 conda-forge
argon2-cffi 20.1.0 py39hcbf5805_2 conda-forge
async_generator 1.10 py_0 conda-forge
attrs 21.2.0 pyhd8ed1ab_0 conda-forge
backcall 0.2.0 pyh9f0ad1d_0 conda-forge
...
Together with import IPython; print(IPython.sys_info())
it is quite enough to know what was ran in that session.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With