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
- 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,reanddatetimeneed no installation; third-party packages likerequests,numpyorpandasmust be installed first with pip in the active environment. - Write a single comma-separated import statement. At the top of the file, put each module name after
import, separated by commas:
This is functionally identical to four separateimport os, sys, json, mathimportlines. The order does not affect behavior, though many style guides prefer alphabetical order for readability. - Add aliases where two libraries clash or a name is long. Combine commas with the
askeyword:
Each alias only applies to the module immediately before it. After this line you callimport numpy as np, pandas as pd, matplotlib.pyplot as pltnp.array(...),pd.DataFrame(...)andplt.plot(...). Note thatimport matplotlib.pyplotimports the submodule and binds the namematplotlibin the namespace, while the alias form bindspltdirectly to the submodule. - Use the
from ... import ...form when importing specific names. To bring several names from one module in one line, separate them with commas:
You can also import several whole modules from a package this way:from datetime import datetime, timedelta, timezone
This is still one statement, one line.from os import path, getcwd, listdir - Combine distinct statements with semicolons only if you really need one physical line. Python allows
import os; import syson 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. - Run the script and confirm the names exist. Execute it, or in the interactive shell try
print(os.getcwd())orprint(np.__version__). If a name is not defined, the import line did not include it or failed silently earlier in a try/except block. - 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:
For plain imports there is no parenthesized comma form; keep them on one line or split into separate statements across lines.from datetime import ( datetime, timedelta, timezone, )
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 nprebindsnpto pandas, silently breaking numpy calls. Give every aliased module a unique name. - Mixing
fromandimportincorrectly.from os, sys import pathis a syntax error. Thefromform takes exactly one module (or dotted package path) afterfrom. - Forgetting the submodule path.
import matplotlib.pyplot as pltworks;import pyplotalone does not, becausepyplotis 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
isortorblackto reorganize imports consistently; both will happily split or merge lines for you. - If two modules share the same name (for example a local
json.pyshadowing 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.