How do I get a list of locally installed Python modules?

You can use the pip module to get a list of locally installed Python modules. Here is a code snippet that does this:

import pip

installed_modules = pip.get_installed_distributions()
module_list = sorted([str(module) for module in installed_modules])

print(module_list)

This code imports the pip module, gets a list of installed distributions (which includes modules), converts the list of distributions to a list of strings, and sorts the list alphabetically. The final list is then printed out.

Note that this code snippet will work in Python 2 and Python 3.