How to Split a Long Import over Several Lines in Python
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
from <module> import (
<name 1>,
<name 2>,
<name 3>,
)
| Part | What it means |
|---|---|
( and ) | Let the list of names continue over several lines |
| One name per line | Easy to read, and adding or removing a name changes one line |
| The comma after the last name | Allowed 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
from math import (
ceil,
floor,
sqrt,
)
print(ceil(2.1), floor(2.9), sqrt(16))

Step 2: Run It
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
| Mistake | What happens | Do this instead |
|---|---|---|
Splitting without brackets: from math import ceil, and floor on the next line | SyntaxError: trailing comma not allowed without surrounding parentheses | Put the names in round brackets |
Using square brackets: from math import [ceil, floor] | SyntaxError: invalid syntax, pointing at the [ | Use ( and ) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
trailing comma not allowed without surrounding parentheses | The name list goes onto a new line without brackets | Wrap 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
- The import statement — the full grammar.
- Explicit line joining — the backslash rule.
