Duplicate Imports

ID

python.duplicate_imports

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Python

Tags

code-style, imports

Description

Reports import statements that bring in the same module — or the same name from a from-import — twice within the same file. Python caches modules, so the second import is harmless at runtime, but it confuses readers and is almost always a leftover of a refactor or merge.

import os
import os                            # FLAW
import os as system                  # FLAW

from collections import OrderedDict
from collections import OrderedDict  # FLAW

from os import path
from os import getcwd                # OK — different name
from os import path                  # FLAW

Rationale

A second import of the same module / name is dead text. It implies that one of the two has a different purpose, sending readers on a wild-goose chase to find the difference; the difference is just churn. Aliases (import json as j) bind the same module — the alias does not save the line.

Remediation

Delete the duplicate line. If you genuinely needed two names for the same module, write from x import a, b once.