Import star

2 snippets across 2 stacks - JavaScript, Python

Also written as star import

JSJavaScript

Namespace Import

JS · Modules
Syntax
import * as name from "./module.js";
Example
import * as validators from "./validators.js";

const email = "[email protected]";
console.log(validators.isEmail(email));
console.log(validators.isNotEmpty(email));

Note Imports all named exports as a single object. The object is frozen (read-only). Useful when a module has many exports you want to use together.

PYPython

__all__ (Controlling Exports)

PY · Modules & Imports
Syntax
__all__ = ['name1', 'name2']
Example
# utils.py
__all__ = ["format_price", "validate_email"]

def format_price(amount: float) -> str:
    return f"${amount:,.2f}"

def validate_email(email: str) -> bool:
    return "@" in email

def _internal_helper():  # not exported
    pass

Note __all__ defines what 'from module import *' exports. It does not prevent direct imports of unlisted names. Prefix internal helpers with underscore by convention.

Frequently asked questions

How do you import star?
This task is covered in 2 stacks on this page: JavaScript, Python. The "Namespace Import" snippet in JavaScript uses `import * as name from "./module.js";`.
Which code does the JavaScript example use?
The "Namespace Import" snippet uses `import * as name from "./module.js";`, from the Modules section of the JavaScript cheat sheet.
Which stacks cover "import star" on this page?
JavaScript, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Namespace Import": Imports all named exports as a single object. The object is frozen (read-only). Useful when a module has many exports you want to use together.