SoFunction
Updated on 2025-04-28

How to detect which dependencies of projects are not used

To detect which dependency libraries in Python projects are not used, you can use the following methods:

1. Use static analysis tools

vulture: Static analysis tool that detects unused code and imports

pip install vulture
vulture your_project/

pyflakes: Check for unused import statements

pip install pyflakes
pyflakes your_script.py

2. Dynamic analysis tools

+ pytest-cov: Generate coverage report after running the test, marking unexecuted code (including import)

pip install coverage pytest-cov
pytest --cov=your_project tests/

3. Dependency Analysis

pipdeptree: List all dependency trees and compare the actual imported libraries

pip install pipdeptree
pipdeptree --reverse | grep -v "^\s"  # Show top-level dependencies

pip-check: Check for installed but not used packages

pip install pip-check
pip-check --unused

4. Automated detection scripts

By comparing and actually imported libraries:

import pkgutil
import subprocess

# Get installed librariesinstalled = { for pkg in pkgutil.iter_modules()}
# Get dependencies for project declarationswith open('') as f:
    required = {('==')[0].strip() for line in f}

unused = required - installed
print(f"Unused dependencies: {unused}")

5. IDE integration

PyCharm/VSCode: Built-in unused import detection (static analysis)

pylint: Provides more comprehensive code quality check

pip install pylint
pylint your_project/

6. Things to note

Indirect dependency: Some libraries may be called implicitly by other dependencies (such as absl-py being depended by TensorFlow), and need to be analyzed in combination with pipdeptree

Dynamic import: importlib.import_module() or __import__() may not be detected by static tools

Test coverage: Make sure the test covers all code paths, otherwise it may false alarms

It is recommended to use a combination of static and dynamic tools such as vulture+ for improved accuracy.

7. Knowledge extension

How to view dependency library in python

There are many ways to view dependent libraries in Python, including using pip list, pip freeze, pip show, and files.Through these methods, you can effectively manage and view all the libraries you need in your project. Taking pip list as an example, this command will list all installed Python packages and their versions.

pip listIt is one of the easiest ways. It displays all installed Python packages and their versions. You just need to enter it in the terminal or command linepip list, you can get a detailed list of all installed packages in the current environment. This command is especially suitable for quick checking installed packages.

To provide a more detailed description of how to view and manage Python dependencies, here are a detailed description and examples of usage of several main methods:

list

pip listIt is the easiest way to view all installed packages in the current environment. It lists all packages and their version numbers.

pip list

This command will output a list showing all installed packages and their versions. This is very useful for quick checking the currently installed libraries.

freeze

pip freezeThe command will output all installed packages and their versions and useFile format display. This is very useful for creating dependency files.

pip freeze

You can redirect the output to a file for later use.

pip freeze > 

This will create aFile, which contains a list of all installed packages. You can use this file to reproduce installed packages in other environments.

show

pip showThe command provides detailed information about a single package, including versions, dependencies, and authors.

pip show <package-name>

For example, to viewrequestsFor package information, you can use the following command:

pip show requests

This will show the relevantrequestsDetails of the package, including its version, location, dependencies, etc.

document

Files are the standard way to record project dependencies. You can usepip freezeThe command generates this file and then uses it to install the same dependencies.

Createdocument:

pip freeze > 

InstallAll packages in the file:

pip install -r 

This will ensure that the same packages and versions as the original environment are installed in the new environment.

5. Use a virtual environment

Virtual environment is one of the best practices for managing Python dependencies. It ensures that each project uses a separate collection of packages to avoid conflicts between packages.

Create a virtual environment:

python -m venv myenv

Activate the virtual environment:

Windows:

myenv\Scripts\activate

MacOS/Linux:

source myenv/bin/activate

Install packages in a virtual environment:

pip install package-name

View packages in a virtual environment:

pip list

Exit the virtual environment:

deactivate

6. Use Pipenv

Pipenv is a more advanced dependency management tool that can replacepipandvirtualenv. It automatically creates and manages virtual environments and usesPipfileandFile to record project dependencies.

Install Pipenv:

pip install pipenv

Install the package using Pipenv:

pipenv install package-name

Activate the Pipenv virtual environment:

pipenv shell

View Pipenv managed packages:

pipenv graph

7. Use Conda

Conda is another package management tool that is especially suitable for data science and machine learning projects. It can manage not only Python packages, but also other dependencies such as C libraries.

Installation package:

conda install package-name

View installed packages:

conda list

Create a virtual environment:

conda create --name myenv

Activate the virtual environment:

conda activate myenv

Manage dependencies:

conda env export > 

conda env create -f

8. Use the IDE and editor

Many integrated development environments (IDEs) and editors such as PyCharm, Visual Studio Code, and Jupyter Notebook provide built-in tools to manage and view Python dependencies.

PyCharm

PyCharm provides graphical package management tools. You can navigate toFile > Settings > Project > Project InterpreterCome to view and manage the package.

Visual Studio Code

VS Code provides Python extensions that can be integrated with virtual environments and pips. You can access these functions through the command panel (Ctrl+Shift+P).

Jupyter Notebook

Jupyter Notebook allows you to run shell commands directly in your notebook unit. For example, you can use!pip listTo view installed packages.

!pip list

9. Dependency diagram

In some complex projects, looking at the dependency graph can help you understand the relationship between packages. Tools such aspipdeptreeDependency diagrams can be generated.

Install pipdeptree:

pip install pipdeptree

Generate dependency diagram:

pipdeptree

This will display a graphical representation of all packages and their dependencies, helping you better understand and manage dependencies.

This is the article about how Python detects projects and which dependencies are not used. For more related Python dependency library detection content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!