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 *
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 …
Have a look at PyChecker. It is a debugging tool and able to find unused variables and modules.
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”.
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.
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
Importchecker is a commandline utility to find unused imports in Python modules.
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)
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!
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 *
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 …
Have a look at PyChecker. It is a debugging tool and able to find unused variables and modules.
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”.
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.
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
Importchecker is a commandline utility to find unused imports in Python modules.
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)
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!