Note Returns a promise that resolves to the module namespace. Ideal for code-splitting, lazy loading, and conditional imports. Works at runtime, not just at the top of a file.
import module
from module import name
import module as alias
Example
import json
from pathlib importPathfrom collections import defaultdict,Counterimport numpy as np # common alias convention
data = json.dumps({"key":"value"})print(data)
Output
{"key": "value"}
Note Convention: standard library imports first, then third-party, then local. Separate groups with a blank line. Use isort to auto-format.
Frequently asked questions
How do you import()?
This task is covered in 2 stacks on this page: JavaScript, Python. The "Dynamic Import" snippet in JavaScript uses `const module = await import("./module.js");`.
Which code does the JavaScript example use?
The "Dynamic Import" snippet uses `const module = await import("./module.js");`, from the Modules section of the JavaScript cheat sheet.
Which stacks cover "import()" on this page?
JavaScript, Python. Together they hold 3 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Dynamic Import": Returns a promise that resolves to the module namespace. Ideal for code-splitting, lazy loading, and conditional imports. Works at runtime, not just at the top of a file.