Search code examples
pythonvisual-studio-code

Is there a way to remove unused imports for Python in VS Code?


I would really like to know if there is some Extension in Visual Studio Code or other means that could help identify and remove any unused imports.

I have quite a large number of imports like this and it's getting close to 40 lines. I know some of them aren't in use, the problem is removing them safely.

from django.core.mail import EmailMultiAlternatives, send_mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from rest_framework import routers, serializers, viewsets, status
from rest_framework.views import APIView
from rest_framework.response import Response
from django.contrib.auth.models import User

Solution

  • Go to the User Settings json file and add the following:

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

    This should remove the unused python imports automatically.

    More suggestions here: How can I check for unused import in many Python files?