Check Python Packages: pip Commands & Dependency Guide

1. Introduction

Using Python for development and data analysis often raises questions like “What packages are currently installed?” and “What are their versions?” Especially when working in a development team or reviving an old project after a long time, verifying installed packages becomes a necessary step. However, there are several ways to manage Python packages, and if you’re not familiar, you might be unsure which command to use. In this article, to meet the need of “checking Python packages,” we’ll clearly explain, aimed at beginners, how to list packages and check versions using pip, as well as tools for visualizing dependencies. We also cover the appropriate verification methods for various development environments such as virtual environments and Anaconda. Whether you’re just starting to use Python seriously or you’re an engineer who uses Python daily, this content will be useful, so please read through to the end.

2. How to Get a List of Packages (by Command)

If you want to view a list of installed Python packages and their versions, you mainly use the “pip command”. Here we explain in detail how to use the representative commands and their output examples.

2.1 How to Use pip list

The most basic method is to enter the following command in a terminal or command prompt.
pip list
Running this command displays a list of all package names and their versions installed in the current Python environment. For example, you get output like the following.
Package      Version
------------ -------
numpy        1.26.4
pandas       2.2.2
requests     2.31.0
Main options:
  • --outdated Shows only packages that can be updated.
  pip list --outdated
  • --not-required Shows only packages that are not referenced by other packages (i.e., not dependencies).
  pip list --not-required
  • --format=columns|freeze|json Allows you to specify the output format. For example, to output in JSON format, do the following.
  pip list --format=json

2.2 How to Use pip freeze

pip freeze is similar to pip list, but outputs package information in a format usable in files like “requirements.txt” (package==version).
pip freeze
For example, the output looks like this.
numpy==1.26.4
pandas==2.2.2
requests==2.31.0
This format is especially useful when you want to reproduce the same package configuration in another environment (rebuilding or sharing environments). Also, you can save it to a file with the following command.
pip freeze > requirements.txt

2.3 How to Use pip show

If you want more detailed information about a specific package, use the pip show command.
pip show package_name
For example, to investigate details of the requests package, run:
pip show requests
Sample output (partial):
Name: requests
Version: 2.31.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Location: /usr/local/lib/python3.11/site-packages
Requires: 
Required-by: 
In this way, you can check various information such as the package description, homepage, dependencies, and installation location. By using these commands appropriately, you can achieve optimal package management for each situation.
侍エンジニア塾

3. How to Check Versions Within a Script

Sometimes you may want to directly check the versions of installed packages within a Python script. This is especially useful for verifying program behavior, detecting environment differences, or writing version‑dependent logic. Below, we introduce the most common methods.

3.1 Using the version attribute

Many Python packages define a __version__ attribute. By using it, you can easily retrieve the version number from code. Example: Displaying NumPy’s version
import numpy
print(numpy.__version__)
When written like this, the NumPy version (e.g., 1.26.4) is printed.

3.2 Note: When version is not defined

The __version__ attribute is not available for every package. In particular, small libraries or older packages may lack it. In such cases, you’ll need to consult the official documentation or try the following approaches.

3.3 Using importlib.metadata (Python 3.8 and newer)

If you are on Python 3.8 or later, you can use the standard library’s importlib.metadata (or, on Python 3.7 and earlier, install importlib_metadata) to obtain version information for installed packages. Example: Getting pandas’ version
from importlib.metadata import version
print(version("pandas"))
This approach works well even for packages without a __version__ attribute, making it a recommended modern practice.

3.4 Practical Use Cases

  • Branch logic based on the version
  • Display a warning when the version is outside the supported range
Example: Version check
import sys
from importlib.metadata import version

if version("numpy") < "1.20.0":
    sys.exit("NumPy 1.20.0 or newer is required. Please check the version.")
Knowing how to flexibly check versions within scripts helps prevent environment issues and improves quality.

4. Visualizing Dependencies: pipdeptree

As Python projects grow, multiple packages end up depending on each other. When the dependency graph becomes complex, it becomes hard to tell “which package needs which,” making updates and troubleshooting more difficult. In such cases, a tool called “pipdeptree” is very helpful.

4.1 What is pipdeptree

pipdeptree is a command‑line tool that displays the dependency tree of installed Python packages. This makes the relationships between packages clear at a glance, which is useful for spotting unnecessary packages or conflicts.

4.2 How to Install pipdeptree

First, install pipdeptree using pip.
pip install pipdeptree

4.3 Basic Usage

After installation, you can view the dependency tree with the following command.
pipdeptree
Sample output (partial):
pandas==2.2.2
  - numpy [required: >=1.24.0,<2.0.0, installed: 1.26.4] - python-dateutil [required: >=2.8.2, installed: 2.8.2]
    - six [required: >=1.5, installed: 1.16.0]
  - pytz [required: >=2020.1, installed: 2023.3]
In this way, you can see hierarchically which packages depend on which.

4.4 Commonly Used Options

  • -r (reverse mode): Shows the top‑level packages that depend on the specified package.
  pipdeptree -r
  • --warn fail/warn/silence: Controls warning output when there are dependency issues (such as conflicts).
  • --json: Outputs the result in JSON format, making it easier to integrate with other tools.

4.5 Advanced Use: Retrieving License Information and Package Management

pipdeptree is useful not only for understanding dependencies but also for removing unnecessary packages and resolving package conflicts. It can also aid in project cleanup and security or license audits, so we recommend becoming familiar with it. If you feel the dependency graph is getting too complex, try running pipdeptree to get an overview. The next section will cover how to inspect packages in virtual environments and conda environments.
RUNTEQ(ランテック)|超実戦型エンジニア育成スクール

5. Virtual Environments and Package Inspection

In Python development, virtual environments (such as venv or virtualenv) and Anaconda (conda environments) are commonly used. Because the installed packages differ between these environments, it is important to accurately know “what is installed in which environment right now.” This section introduces how to check packages in virtual environments and conda environments.

5.1 When Using Virtual Environments (venv, virtualenv)

When the virtual environment is activated, using the pip list, pip freeze, or pip show commands introduced earlier will display only the package information within that virtual environment. 【Basic workflow for virtual environments】
  1. Create a virtual environment
   python -m venv myenv
  1. Activate the virtual environment (Windows)
   myenvScriptsactivate
(macOS/Linux)
   source myenv/bin/activate
  1. Check the package list
   pip list
This allows you to safely manage different package configurations for each project.

5.2 When Using a conda Environment

If you are using Anaconda or Miniconda, it is common to use the conda command for package management.
  • Activating a conda environment
  conda activate myenv
  • Checking the package list
  conda list
Running conda list displays a list of all packages and their versions installed in that environment. You can also check a specific package as follows.
conda list package_name

5.3 Points to Note

  • Be careful: if you run commands without activating the virtual or conda environment, the packages from the global environment will be shown.
  • Separating environments per project helps prevent package conflicts and runtime issues.
By managing packages correctly for each environment and being able to check them when needed, Python development becomes more comfortable.

6. Summary

When developing with Python, understanding the installed packages and their versions is extremely important from the perspectives of project management and preventing issues. This article systematically explained methods for checking packages, providing useful information for beginners through intermediate users. First, by using basic commands such as “pip list”, “pip freeze”, and “pip show”, you can easily obtain a list of packages and detailed information. Additionally, we introduced how to check the version of a specific package from within a script using the __version__ attribute or importlib.metadata. Also, when package dependencies become complex, using “pipdeptree” helps you visualize the overall structure as a tree. Moreover, in virtual environments or conda environments, you can manage package information accurately by using the commands specific to each environment. By choosing the appropriate method, package management becomes more efficient and safer. In particular, for projects with multiple developers or systems that require long‑term maintenance, regularly checking packages helps prevent problems. Be sure to take advantage of the commands and tools introduced here to create a more comfortable Python development environment.

7. FAQ (Frequently Asked Questions)

Here we have compiled the frequently asked questions and answers regarding “Python package verification”. Please refer to this when you encounter problems or have questions.

Q1. How can I check all installed Python packages at once?

pip list
or
pip freeze
Running it will list all installed packages and their versions.

Q2. How can I check the version of a specific package?

pip show package_name
will provide detailed information about the specified package. Alternatively, within a Python script
import package_name
print(package_name.__version__)
you can write this to output the version (however, some packages do not provide a __version__ attribute).

Q3. How can I view package dependencies in a tree format?

Install the tool called pipdeptree and
pipdeptree
Running it will display the dependency tree of installed packages.

Q4. Does the method differ for virtual environments or Anaconda (conda) environments?

If you activate a virtual environment and use pip list or pip freeze, only the packages installed in that virtual environment will be shown. In the case of Anaconda/Miniconda, use the
conda list
command.

Q5. Can I also view license?

pip show package_name will display license information for some packages. Additionally, by combining pipdeptree and other external tools, you can perform bulk license checks.

Q6. How to troubleshoot when pip commands are not working properly?

Often this is because Python or pip itself is not installed, or there is a problem with the PATH configuration.
  • Try running with “python -m”, e.g., python -m pip list
  • Upgrade pip (python -m pip install --upgrade pip)
  • Check that the virtual environment or Anaconda environment is correctly active, etc. Please try these.

Q7. How can I save package information to a file?

pip freeze > requirements.txt
Running this command will save the current list of packages to a file.
侍エンジニア塾