73

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 *

Thierry Lam
  • 45,304
  • 42
  • 117
  • 144

12 Answers12

70

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
doug
  • 69,080
  • 24
  • 165
  • 199
15

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 ...

0 _
  • 10,524
  • 11
  • 77
  • 109
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
15

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
Waket Zheng
  • 5,065
  • 2
  • 17
  • 30
11

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)
Keiku
  • 8,205
  • 4
  • 41
  • 44
6

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

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
5

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.
Paul Thompson
  • 1,071
  • 1
  • 8
  • 6
3

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.

2

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
1

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.

Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
1

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

maciek
  • 3,198
  • 2
  • 26
  • 33
  • 1
    Seems to be unmaintained and does not work with relative imports https://github.com/zopefoundation/importchecker/issues/4 – Wes Feb 25 '20 at 15:07
0

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".

sinapan
  • 948
  • 1
  • 9
  • 23
0

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!

HadiAlqattan
  • 413
  • 5
  • 13