'Get list of all loaded python packages & versions, and variables

I'm coming from an R background where it was quite easy to figure out for me all of the loaded packages that I was using. I could look to my rstudio tab 'packages' and see checkmarks next to package names and versions for what I had loaded / available.

I could run:

library(dplyr)
x=3;
y=4;

then run to find out the packages I had loaded('dplyr): sessionInfo() and to find out which variables were in my environment if I didn't want to look at the loaded variable pain (x, y)

ls() 

I'm trying to figure out a way to do this in python. For example, is there a command that will:

  1. Show me everything available python package I can import & corresponding versions?
  2. Show me the packages I have already loaded thus far in my session and their corresponding versions?
  3. Show me all my defined variables that I have made and their type (such as pandas data frames, lists, dictionaries, functions, etc)

I'm sure this is pretty easy, but I can't seem to find how to do this coming from R where it was very simple.



Solution 1:[1]

Show me everything available python package I can import & corresponding versions?

help('modules')

NB: You can also type pip freeze on the command line to see which third-party modules you have installed using pip. (Use python -m pip freeze if you have multiple Python versions and want to be sure you list the packages for the same version as python.) However, this list is not complete, since it does not contain built-in modules and your own modules.

Show me the packages I have already loaded thus far in my session and their corresponding versions?

I don't think this is possible, but see next question:

Show me all my defined variables that I have made and their type (such as pandas data frames, lists, dictionaries, functions, etc)

globals()

This also includes all loaded modules. You could filter out the 'modules' if you want.

Solution 2:[2]

You can get the currently loaded modules with sys.modules. This means you can use the following code to print the currently loaded modules and their versions:

import sys

for module in sys.modules.values():
    if hasattr(module, "__version__"):
        print(module.__name__, module.__version__)
    else:
        print(module.__name__)

Modules aren't obliged to provide a __version__ attribute, so you have to check whether it exists before you use it. Most modules in the standard library don't provide a version, although interestingly, some provide versions that are different from the current version of Python.

A caveat is that you can use importlib to load modules without adding them to sys.modules, so there is no guarantee that sys.modules will be accurate 100% of the time.

Solution 3:[3]

1. Not possible in general. Import statements are handled dynamically at the time the line is executed, and so the result depends on whatever is lying around on your filesystem at that time.

You can get a decent idea of what's available by looking in the site-packages dir, the location of which is platform dependent but can be found with the code snippet below:

>>> import sysconfig
>>> sysconfig.get_path("purelib")
'/usr/local/lib/python3.7/site-packages'

The standard library is always available. A comprehensive list of stdlib modules is in the library docs here: https://docs.python.org/3/library/

There is a helper function in pkgutil.walk_packages() which will essentially walks the sys path and looks for accessible modules. I believe pydoc's help("modules") listed in the other answer actually uses this helper internally.

pip freeze will show distributions installed in a virtual environment.

2. Just look in sys.modules. A version number for a module is not required, and might not even exist, but a common convention is for the top-level module to have a __version__ attribute.

3. This is built-in function vars. Just call vars() with no arguments, it will return a mapping of names and objects. The types of the objects you will just have to see with type(obj).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wovano
Solution 2 Jack Taylor
Solution 3