You can import multiple libraries in Python on one line by separating them with commas in a single import statement, for example import os, sys, json. This imports each module and binds all three names in the current namespace with one statement. It works for the plain import form, and there are similar one-line tricks for from ... import ... and for aliasing, covered below. Python reads the line left to right, so all the usual rules about naming, aliases and namespace collisions still apply.

What You'll Need

  • A working Python installation (any recent Python 3 version) and access to a terminal or command prompt.
  • A text editor or IDE to write and run your script.
  • The libraries you intend to import installed in the same environment you are running the script from, or confirmed as part of the standard library.
  • Basic familiarity with running a Python file (python script.py) or the interactive shell.

Step-by-Step

  1. Decide which libraries you need. List the modules you actually use in the script. Importing things you never touch adds startup time and clutters the namespace. Standard-library modules such as os, sys, json, math, re and datetime need no installation; third-party packages like requests, numpy or pandas must be installed first with pip in the active environment.
  2. Write a single comma-separated import statement. At the top of the file, put each module name after import, separated by commas:
    import os, sys, json, math
    This is functionally identical to four separate import lines. The order does not affect behavior, though many style guides prefer alphabetical order for readability.
  3. Add aliases where two libraries clash or a name is long. Combine commas with the as keyword:
    import numpy as np, pandas as pd, matplotlib.pyplot as plt
    Each alias only applies to the module immediately before it. After this line you call np.array(...), pd.DataFrame(...) and plt.plot(...). Note that import matplotlib.pyplot imports the submodule and binds the name matplotlib in the namespace, while the alias form binds plt directly to the submodule.
  4. Use the from ... import ... form when importing specific names. To bring several names from one module in one line, separate them with commas:
    from datetime import datetime, timedelta, timezone
    You can also import several whole modules from a package this way:
    from os import path, getcwd, listdir
    This is still one statement, one line.
  5. Combine distinct statements with semicolons only if you really need one physical line. Python allows import os; import sys on a single line because the semicolon separates simple statements. This is valid syntax but discouraged by PEP 8, the official style guide. It saves no meaningful time and hurts readability. Prefer the comma form; use semicolons only in quick one-off shell snippets.
  6. Run the script and confirm the names exist. Execute it, or in the interactive shell try print(os.getcwd()) or print(np.__version__). If a name is not defined, the import line did not include it or failed silently earlier in a try/except block.
  7. Style it for longer lists. For many modules, wrap the statement in parentheses so it can span multiple lines while remaining a single logical statement:
    from datetime import (
        datetime,
        timedelta,
        timezone,
    )
    For plain imports there is no parenthesized comma form; keep them on one line or split into separate statements across lines.

Menu labels and editor behaviors can differ slightly by version, device or region, but Python's import syntax itself is stable across Python 3 releases.

Common Mistakes

  • Assuming a shared alias. Writing import numpy as np, pandas as np rebinds np to pandas, silently breaking numpy calls. Give every aliased module a unique name.
  • Mixing from and import incorrectly. from os, sys import path is a syntax error. The from form takes exactly one module (or dotted package path) after from.
  • Forgetting the submodule path. import matplotlib.pyplot as plt works; import pyplot alone does not, because pyplot is a submodule, not a top-level package.
  • Believing one line is faster. Import cost comes from executing module code, not from counting statements. A comma-separated line does not load faster than separate lines.
  • Leaving unused imports. Tools like flake8 will flag them, and they can mask circular-import problems.
  • Relying on import *. from math import * dumps every public name into your namespace and can overwrite your own variables. Name what you need explicitly.

Tips

  • Keep all imports at the top of the file, grouped: standard library first, then third-party packages, then your own modules, separated by blank lines. PEP 8 describes this layout.
  • Put imports inside a function only when you need to defer a heavy or optional dependency, not as a default habit.
  • Sort names alphabetically within a line so diffs stay clean when you add or remove one.
  • Use an auto-formatter such as isort or black to reorganize imports consistently; both will happily split or merge lines for you.
  • If two modules share the same name (for example a local json.py shadowing the standard library), an alias plus a clear package path avoids confusion.

Troubleshooting

If that did not work, start with the simplest checks. A ModuleNotFoundError almost always means the package is not installed in the interpreter running your script. Confirm with python -m pip show requests (substitute your package). If it is installed elsewhere, you may be running a different interpreter or virtual environment than the one where pip installed it; activate the right environment or run installation with python -m pip install ... to target the correct interpreter.

A SyntaxError pointed at the import line usually means a stray comma at the end outside parentheses, a missing comma, or a misplaced from. Read the caret position: Python marks the first token it could not parse.

An ImportError of the form cannot import name 'x' from 'y' means the name does not exist in that module, often because your installed version is older than the one your code assumes. Check the installed version and the project's documentation, then upgrade with pip if appropriate.

If your own file shadows a library, for example a file named random.py in the project folder, Python will import that file instead. Rename it and delete any stale __pycache__ folder. Avoid naming scripts after modules you import.

Circular imports happen when two modules import each other at the top level. The fix is to move one import inside a function or restructure shared code into a third module; do not simply reorder lines and hope.

Frequently Asked Questions

Can I import multiple libraries with one import statement?

Yes, using commas: import os, sys, json. Each module becomes available under its own name.

Does importing on one line run faster than separate lines?

No meaningful difference. The cost is loading and executing the modules, not the number of statements.

How do I import several modules with aliases at once?

Repeat the pattern per module: import numpy as np, pandas as pd. Each alias binds only to the module it follows.

Can I mix import a and from b import c in one statement?

No. Those are different statement forms. Put them on separate lines, or use a semicolon to place them on one physical line, though separate lines are clearer.

Is a semicolon between imports good style?

It is valid but PEP 8 discourages it. Use it only for quick interactive snippets.

Why does my local file get imported instead of the real library?

Because the current directory takes precedence on the import path. Rename any script that matches a module name, such as json.py or random.py.