Explain Codes LogoExplain Codes Logo

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

python
prompt-engineering
venv
pandas
Anton ShumikhinbyAnton Shumikhin·Oct 11, 2024
TLDR

Fetch your list of installed Python packages with a bald eagle-like speed using pip:

pip freeze

This outputs installed packages and their versions like a supermarket receipt, ready for a swift copy-paste into a requirements.txt file.

But, what if you're more of a human and less of a machine? You'd probably prefer it in English instead:

pip list

Nice and clean, just a casual list of everything installed, without the fuss.

Backstage of Python packages

Chatting with Python

You're in the middle of a Python shell and need to check which modules are in the wing, waiting to make an entrance? No worries, Python's got a backstage pass for you:

help('modules')

The above help command sends Python on a treasure hunt for all accessible packages, making it your trusty guide on this module safari in the Python jungle.

Python script to the rescue

For those who love a little automation, pkgutil brings your Python hero:

import pkgutil installed_packages = [module for _, module, _ in pkgutil.iter_modules()] print(installed_packages) # Installation complete, time for a cup of coffee ☕

This snappy script skips the human-talk and provides a Python readable format, ready for future analysis or custom reporting. Why do it manually when pkgutil can do it for you?

Compatibility equation

The scary movie called dependencies strikes again. Pip's get_installed_distributions won't go well with versions of pip greater than 10.0. Before getting into a mud fight with your terminal, consider the superhero pkg_resources as an alternative. Trust me, it saves a lot of cleaning afterward.

Decoding the Python package managers

setuptools, pip, easy_install decoded

Here be the Python installers: setuptools, pip, or easy_install. They keep meticulous records of metadata for each installation, kind of like your mom after a shopping spree.

They're ideal for Flask servers where pip makes debugging feel like a walk in a debug park by providing extremely detailed package lists.

import pkg_resources installed_packages_list = sorted(["%s==%s" % (i.key, i.version) for i in pkg_resources.working_set]) print(installed_packages_list) # Watch your step! You might trip over those version numbers. 😉

Ghost packages

Beware of the ghost packages. Some libraries, haunted with a local setup.py, might not be recognized by Python's ghostbusters. If things start floating around, it's probably a bug, not a poltergeist.

Python terminal and autocompletion tricks

Using IPython and Python interpreter

In the IPython universe, the Tab key is your autopilot. Type import and let Autocomplete do its magic:

import <ModuleName> # Hit Tab key. Watch your fingers! ✨

Terminal tricks

Stuck in a terminal without Python? pydoc to the rescue:

pydoc modules

Not everything is lost in the terminal. pydoc might not bring you a chocolate, but it will present you a fancy listing of the standard library modules.

Sealing the deal with pip list and pip freeze

Finding the right tool

Pip list and pip freeze are pretty much like night and day. Use pip list for a casual stroll among your installed packages. Bring out pip freeze when you need to get serious about your versions and dependencies.

Mastering the nuances

Limited visibility

Pip freeze might not guarantee an X-ray vision into all your non-standard libraries. So, here's your chance to play detective and discover the unseen manually installed or customized packages.

Continuous vigilance

While you're busy coding, Pip versions are having a party. So, stay vigilant because pip update hangovers might impact your forwards or backwards package compatibility.

A season of mysteries

Packages missing from your list might not have caught the Hogwarts express. Or they might be hinting at a deeper installation issue that may require a bit more of your wizardry.