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

Python

Python Problem Overview


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 *

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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)

Solution 4 - Python

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

Solution 5 - Python

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

I recently found [autoflake][2]:

  • Uses [pyflakes][1] for checking.
  • Defaults to removing unused standard library imports and redundant pass statements.
  • Has options for removing other unused imports and unused variables.

[1]: https://pypi.python.org/pypi/pyflakes "pyflakes" [2]: https://pypi.python.org/pypi/autoflake/ "autoflake"

Solution 6 - Python

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 -rc -sl .
autoflake --remove-all-unused-imports -i -r .
isort -rc -m 3 .

Solution 7 - Python

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.

Solution 8 - Python

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.

Solution 9 - Python

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.

Solution 10 - Python

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

Solution 11 - Python

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

Solution 12 - Python

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThierry LamView Question on Stackoverflow
Solution 1 - PythondougView Answer on Stackoverflow
Solution 2 - PythonChristopheDView Answer on Stackoverflow
Solution 3 - PythonKeikuView Answer on Stackoverflow
Solution 4 - PythonFelix KlingView Answer on Stackoverflow
Solution 5 - PythonPaul ThompsonView Answer on Stackoverflow
Solution 6 - PythonWaket ZhengView Answer on Stackoverflow
Solution 7 - Pythonuser297250View Answer on Stackoverflow
Solution 8 - Pythonishandutta2007View Answer on Stackoverflow
Solution 9 - PythonAminah NurainiView Answer on Stackoverflow
Solution 10 - PythonmaciekView Answer on Stackoverflow
Solution 11 - PythonsinapanView Answer on Stackoverflow
Solution 12 - PythonHadiAlqattanView Answer on Stackoverflow