Skip to content

Copy with imports

Copy with imports copies the selected Python code and prepends the import statements that are referenced inside that selection.

How to use it

Select Python code, then run Edit | Copy Special | Copy with Imports.

import typing as t
from pydantic import BaseModel

class Model(BaseModel):
    field: t.Annotated[int, None]
import typing as t
from pydantic import BaseModel

class Model(BaseModel):
    field: t.Annotated[int, None]

Notes

  • The selected text is copied as-is, with only trailing whitespace trimmed.
  • Import statements already inside the selection are not duplicated.
  • The feature is gated by the Copy with imports setting under Settings | BetterPy | Productivity Actions | Copying.