Search code examples
python-3.ximportdjango-4.0autoflake

How can I auto-remove unused imports using autoflake for my Python/Django4 project?


I would like to auto-remove unused variables and imports in my Django 4/Python 3.9 project. I have installed autoflake (3.1.0). In my service file, I have a line like

from ..settings import PYTHON_ENV

which VSCode tells me

"PYTHON_ENV" is not accessedPylance

So I ran the autoflake command

autoflake --in-place --remove-unused-variables cbapp/services/my_service.py

but the unused imports remains. What else do I need to do to have the unused imports removed?


Solution

  • It says in the README that by default it only removes unused imports from the standard library. In order to remove all imports you need to give the --remove-all-unused-imports flag.

    https://github.com/PyCQA/autoflake#advanced-usage

    autoflake --in-place --remove-unused-variables --remove-all-unused-imports cbapp/services/my_service.py