How can I check for unused import in many Python files?

Question:

I remember when I was developing in C++ or Java, the compiler usually complains for unused methods, functions or imports. In my Django project, I have a bunch of Python files which have gone through a number of iterations. Some of those files have a few lines of import statement at the top of the page and some of those imports are not used anymore. Is there a way to locate those unused imports besides eyeballing each one of them in each file?

All my imports are explicit, I don’t usually write from blah import *

Asked By: Thierry Lam

||

Answers:

Use a tool like pylint which will signal these code defects (among a lot of others).

Doing these kinds of ‘pre-runtime’ checks is hard in a language with dynamic typing, but pylint does a terrific job at catching these typos / leftovers from refactoring etc …

Answered By: ChristopheD

PyFlakes (similar to Lint) will give you this information.

pyflakes python_archive.py

Example output:
python_archive.py:1: 'python_archive2.SomeClass' imported but unused
Answered By: doug

Have a look at PyChecker. It is a debugging tool and able to find unused variables and modules.

Answered By: Felix Kling

If you use the eclipse IDE with pydev and mylyn, it provides automatic checking and highlighting for unused imports, among other things. It integrates with pylint as well.

Answered By: user297250

I have been using pyflakes successfully and wished to auto-remove the unused imports.

I recently found autoflake:

  • Uses pyflakes for checking.
  • Defaults to removing unused standard library imports and redundant pass statements.
  • Has options for removing other unused imports and unused variables.
Answered By: Paul Thompson

You can use the following user setting:

"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
    "--enable=W0614"
]

But I think, you’ll need to go through all files yourself and hit “save”.

Answered By: sinapan

autoflake is an improved version of pyflakes with additional options.
It uses pyflakes under the hood, I would recommend to use it instead of using pyflakes directly.

Answered By: ishandutta2007

I agree with using PyFlakes. It’s like linting, but it excludes styling errors.

UPDATE

How to run: pyflakes <your python file> or pyflakes <your folder containing python files>

BE CAREFUL!!!

If you run it just with command pyflakes, it takes a really long time like it is never-ending. My hypothesis is it is trying to check every python file in your machine/folder when you call it that way.

Answered By: Aminah Nuraini

I use flake8 to check the style, and then isort+autoflake to auto remove the unused imports.

Check: See more at flake8 vs pyflake

pip install flake8 --user
flake8 .

Reformat: see more at why isort+autoflake

pip install isort autoflake --user
isort -sl .
autoflake --remove-all-unused-imports -i -r .
isort -m 3 .

Recently(since 2023), I use ruff to replace autoflake/flake8

pip install --upgrade --user ruff
ruff --fix /path/to/file_or_folder
Answered By: Waket Zheng

Importchecker is a commandline utility to find unused imports in Python modules.

Answered By: maciek

You can also consider vulture as one of several options.

Installation

pip install vulture  # from PyPI

Usage

vulture myscript.py

For all python files under your project.

find . -name "*.py" | xargs vulture | grep "unused import"

Example

Applies to the code below.

import numpy as np
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

The results are as follows.

➜ vulture myscript.py
myscript.py:1: unused import 'np' (90% confidence)
myscript.py:4: unused variable 'df' (60% confidence)
Answered By: Keiku

You can easily use pycln to do that, just do:

pip3 install pycln
pycln path_of_your_file.py -a

And then all the unused imports are going to be removed!

Answered By: HadiAlqattan
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.