Autocompleting Python Modules

Simplifying the search for modules to execute from a shell.

The last few times I overhauled an execution environment I required people to execute the bulk of their tools via python -m package.module instead of python package/ (to enable the development environment).

The downside is that you lose shell autocompletion, which can be a big deal if you have dozens of tools that you only occasionally use.

This addition to your ~/.bashrc fixes that.

Essentially, it walks sys.path (or the __path__ of the most specific package given) looking for modules that match. Packages are only listed if a __main__ module exist within them.

For example, wsgi completes to wsgiref., and then:

$ python -m wsgiref.

Warning: This does import the most specific package given (e.g. autocompleting email.mime will import it, and therefore may have side effects).

Happy scripting!

Posted . Categories: .