doctorlove
doctorlove

Reputation: 19252

How can I break an import line in Python?

There are various questions about line continuations in Python, e.g., here, here and here, most pointing at the guidelines:

Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent

Most of the specifics are around a long if statement, which can use parenthesis or implicit continuations if calling a function.

This begs the question, how should you deal with import statements? Specifically, what else can I do with

from concurrent.futures import \
  ProcessPoolExecutor

Is a line continuation my only option?

Upvotes: 13

Views: 9704

Answers (2)

Jayanth
Jayanth

Reputation: 142

If you have:

from a.b.c.d.e import f

you can change this to:

from a.b.c.\
    d.e import f

I learnt this from a colleague of mine.

Upvotes: 5

Harrison
Harrison

Reputation: 5396

If you're only importing 1 thing from the package, you should continue to do it the way that you currently are.

If you're importing multiple things, do something like this:

from package_name import (
    x,
    y,
    z,
)

Upvotes: 25

Related Questions