Skip to main content

How to Split a Long Import over Several Lines in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

When you import many names from one module, the line grows past a comfortable width. Python lets you split it: put the names in round brackets, and each can go on its own line. This guide imports three functions that way on Python 3.15.2.

Syntax​

Syntax
from <module> import (
<name 1>,
<name 2>,
<name 3>,
)
PartWhat it means
( and )Let the list of names continue over several lines
One name per lineEasy to read, and adding or removing a name changes one line
The comma after the last nameAllowed inside the brackets

The Python language reference gives the bracketed form in the grammar of the import statement: "from" relative_module "import" "(" identifier ["as" identifier] ("," identifier ["as" identifier])* [","] ")". The [","] before the closing bracket is the optional trailing comma.

The Example​

The script imports ceil, floor and sqrt from the math module over several lines and uses all three.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

long_import.py
from math import (
ceil,
floor,
sqrt,
)

print(ceil(2.1), floor(2.9), sqrt(16))

The long_import.py script in a macOS-style code window: from math import with ceil, floor and sqrt on separate lines inside round brackets, then a print that uses all three.

Step 2: Run It​

Terminal
uv run python long_import.py
Output
3 2 4.0

The terminal after uv run python long_import.py: 3 2 4.0.

All three names were imported: ceil(2.1) rounded up to 3, floor(2.9) rounded down to 2, and sqrt(16) gave 4.0. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Splitting without brackets: from math import ceil, and floor on the next lineSyntaxError: trailing comma not allowed without surrounding parenthesesPut the names in round brackets
Using square brackets: from math import [ceil, floor]SyntaxError: invalid syntax, pointing at the [Use ( and )

Troubleshooting​

SymptomLikely causeFix
trailing comma not allowed without surrounding parenthesesThe name list goes onto a new line without bracketsWrap the names in ( )

FAQ​

Can I use a backslash instead?​

Yes: from math import ceil, \ with floor on the next line also worked and printed 3 2. But a backslash must be the last character on its line: with a space after it, Python stopped with SyntaxError: unexpected character after line continuation character. Brackets don't have that trap.

Does this work with import ... as?​

Yes, the grammar allows as inside the brackets. sqrt as square_root, on its own line inside from math import ( ... ) worked: square_root(16) printed 4.0.

Conclusion​

Round brackets let from math import (...) list ceil, floor and sqrt one per line, with a trailing comma, and all three worked. Without brackets, the same split is a SyntaxError.

Additional Resources​