Multiple Imports on One Line? Python Style Guide

How-to basics · Updated November 23, 2026

Yes, you can put multiple imports on one line in Python, but the official style guide (PEP 8) recommends against it for readability, except for a few specific cases. This article explains the rules, the exceptions, and how to decide what’s best for your code.

What the Python Style Guide Says

PEP 8, the official style guide for Python code, has a clear section on imports. It states: “Imports should usually be on separate lines.” The guide gives this example of what not to do:

import sys, os

And this as the correct alternative:

import os
import sys

The reasoning is simple: separate lines make each import easier to spot, easier to version-control (a change to one import doesn’t touch another line), and easier to read when scanning a file. However, the guide also notes an exception: you can write from subprocess import Popen, PIPE on one line because it’s a single from statement importing multiple names from the same module. That’s considered perfectly fine.

Why Separate Lines Are Preferred

Readability is the main argument. When you scan a file, your eye can quickly pick out each imported module if it’s on its own line. With multiple imports on one line, you have to parse the line more carefully. This becomes especially important in larger files or when reviewing code in a diff. Imagine a change that adds os to a line that already has sys, re, json – the diff shows a modification to that line, but it’s not immediately obvious which module was added. With separate lines, the diff clearly shows a new line added.

When It’s Acceptable to Use One Line

There are a few situations where putting multiple imports on one line is either acceptable or even preferred:

  • From-imports of multiple names: from math import pi, sqrt is standard and encouraged. It’s a single logical statement bringing in several names from one module.
  • Very short, closely related imports: Some developers use import sys, os in quick scripts or interactive sessions, but this is a matter of taste and often frowned upon in professional code.
  • Inside a try/except block: When you need to import a fallback module, you might write try:
    import simplejson as json
    except ImportError:
    import json
    – but here each import is still on its own line.

In general, if you’re writing code that others will read or maintain, stick to one import per line for import statements. The exception for from ... import ... is widely accepted.

How to Format Imports Properly

Beyond one-versus-many lines, Python has a few other rules about import formatting that matter just as much:

  • Put all imports at the top of the file, after the module docstring and before any other code.
  • Group imports in this order: standard library, third-party, then local application imports. Separate each group with a blank line.
  • Sort imports alphabetically within each group (this is what tools like isort enforce).
  • Avoid wildcard imports (from module import *) because they pollute the namespace and make it unclear what names are available.

Here’s an example of a well-formatted import block:

import os
import sys

import requests

from mypackage import helper

Common Problems With Multiple Imports on One Line

Putting multiple imports on one line can cause a few practical issues:

  • PEP 8 violations: Linters like flake8 will flag import sys, os with error E401. If you use a linter, it will fail your checks.
  • Version control noise: As mentioned, a change to one import on a shared line makes the whole line appear changed in a diff, which can obscure what actually changed.
  • Harder to read in large files: When a file has dozens of imports, cramming them together makes the top of the file a dense block that’s hard to scan.
  • Potential for merge conflicts: If two developers add different imports to the same line, they’ll get a merge conflict. On separate lines, the conflict is less likely.

Tools That Enforce Import Style

If you want to automate the decision, several tools can format your imports for you:

  • isort: Sorts imports alphabetically and separates them into sections. It can also split multiple imports on one line into separate lines if you configure it.
  • Black: The popular code formatter will automatically split import sys, os into two lines. It does not merge separate lines into one.
  • flake8: A linter that will report E401 (multiple imports on one line) as an error.
  • ruff: A fast linter and formatter that also enforces import style.

Using these tools in your editor or pre-commit hooks ensures consistency across a team.

Alternatives and Related Practices

If you’re worried about the verbosity of multiple import lines, consider these alternatives:

  • Import modules, not names: Instead of from math import pi, sqrt, sin, you can do import math and then use math.pi, math.sqrt. This can make the code clearer about where a function comes from.
  • Alias long module names: Use import numpy as np to keep code concise without sacrificing readability.
  • Use __all__ in your own modules: This controls what’s exported when someone does from module import *, but it doesn’t change how you write imports.

In short, the only time multiple imports on one line is truly recommended is when you’re using a from ... import ... statement to bring in several names from the same module. For standard import statements, separate lines are the Pythonic way.

Frequently Asked Questions

Is it bad to write import sys, os in Python?

It’s not a syntax error, but it violates PEP 8 and will be flagged by most linters. It’s considered bad practice because it hurts readability and makes version control diffs less clear. The recommended style is to write them on separate lines.

Can I put multiple from imports on one line?

Yes, from math import pi, sqrt is perfectly fine and even encouraged. The rule against multiple imports on one line applies specifically to import statements, not to from ... import ... statements that import multiple names from the same module.

What does E401 mean in flake8?

E401 is the error code for “multiple imports on one line.” Flake8 raises this when you write something like import os, sys. To fix it, split the imports onto separate lines.

Does Black format multiple imports on one line?

Yes, Black will automatically split import sys, os into two lines. However, it will not merge separate lines into one, and it leaves from math import pi, sqrt as is because that’s allowed.

Is there any performance benefit to putting imports on one line?

No. The Python interpreter processes each import statement anyway, and the bytecode is essentially the same. The only difference is readability and maintainability. There is no measurable performance gain from combining imports on one line.

What about importing multiple modules in a single line in a try block?

Even in a try/except, you should keep each import on its own line. For example, try:
import simplejson as json
except ImportError:
import json
is the standard pattern. You can’t combine them because the fallback logic requires separate statements.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.