Use the session-info CLI program (see session-info -h) or use it
programmatically.

Import whayever your script/notebook relies on, as well as session_info.
