Modules and import in ES6 for Python developers
Here's a comparison of Python and JavaScript (ES6) imports. There are a few differences between the two:
- JavaScript imports are static; Python's are dynamic.
- JavaScript items must be explicitly exported. In Python, all items are available for import.
- JavaScript has a concept of a default export. Python does not.
Python | ES6 (ES 2015) |
---|---|
import mymodule
mymodule.myfunc()
def myfunc(): pass
import mymodule as myalias
myalias.myfunc()
def myfunc(): pass
| Namespaced importsimport * as myalias from "./mymodule";
myalias.myfunc();
export function myfunc() {}
import mymodule and import mymodule as myalias forms. |
from mymodule import myvar, myfunc
print myvar
myfunc()
myvar = 42
def myfunc(): pass
| Named importsimport { myvar, myfunc } from "./mymodule";
console.log(myvar);
myfunc();
export var myvar = 42;
// no semicolon for inline exports
// of functions and classes
export function myfunc() {}
|
No equivalent | Default imports (preferred form)import myalias from "./mymodule";
myalias();
export default function myfunc() {}
Note: this import syntax has no curly braces because import mydefault, { myother } from "./mymodule";
mydefault();
myother();
export function myother() {}
export default function myfunc() {}
|
from mymodule import myfunc as myalias
myalias()
def myfunc(): pass
| Renaming an importimport { myfunc as myalias } from "./mymodule";
myalias();
export function myfunc() {}
|
from mymodule import *
print myvar
myfunc()
myvar = 42
def myfunc(): pass
| No equivalent |
from mydir.mymodule import myfunc
myfunc()
def myfunc(): pass
__init__.py file and is a module (package) | Importing from a subdirectoryimport { myfunc } from "mydir/mymodule";
myfunc();
export function myfunc() {}
|
Names vs. paths¶
Modules can be referenced by name or by path. Names are often used with external libraries. For example, below "react" is the name.
import React from "react";
Paths are often used with your project modules. Here the module is referenced by the path "./MyComponent". (The .js
extension is implicit.)
import MyComponent from "./MyComponent";
When are curly braces needed?¶
Curly braces are needed when importing non-default exports from a module. If the item is exported with default
, use the import syntax without curly braces.
// mymodule.js
export default Something;
import Something from "mymodule";
If the item is exported without default
, you must import the item with curly braces.
// mymodule.js
export Something;
import { Something } from "mymodule";
References¶
Related posts
- How to use ast-grep with GraphQL — posted 2024-09-24
- Next.js App Router (RSC) projects w/ open source code — posted 2024-07-30
- Next.js Relay GraphQL Pokemon example — posted 2024-05-22
- Example Node.js Passport.js SAML app using OneLogin — posted 2024-05-10
- Aphrodite to CSS Modules codemod — posted 2022-12-09
- Simple codemod example with jscodeshift — posted 2021-05-03
Comments
Can you explain when you need the curly braces {} in ES6 imports?
disqus:2448577586
Hi Trung, thanks for your comment! Curly braces are needed when importing non- default exports from a module. If the module uses the default keyword when exporting an item, do not use curly braces when importing. If it does not use the default keyword when exporting, do use the curly braces when importing. I'll update the post to add notes about this.
disqus:2448633583
Just found this, trying to learn ES6 right now and keep finding modules somewhat awkward to work with. Like Python, but not. This article clarified a lot for me - thank you!
disqus:3411106622