Skip to main content

Create and Import Modules

A large application is usually composed of many modules, which are independent units of code organization in software development. Each .py file is a Python module, and each .js file is a JavaScript module.

Task

Design a program that defines a calculator module, which provides basic arithmetic operations such as addition, subtraction, multiplication, and division. Import this module in another module, calculate and print the result of 1234 * 4321.

JavaScript implementation

calculator.js
export function add(a, b) {
return a + b;
}

export function subtract(a, b) {
return a - b;
}

export function multiply(a, b) {
return a * b;
}

export function divide(a, b) {
if (b === 0) {
throw new Error("Cannot divide by zero");
}
return a / b;
}
main.js
import { multiply } from './calculator.js';
console.log(multiply(1234, 4321));

Python implementation

calculator.py
def add(a, b):
return a + b

def subtract(a, b):
return a - b

def multiply(a, b):
return a * b

def divide(a, b):
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
main.py
import calculator
result = calculator.multiply(1234, 4321)
print(result)

Code Highlight

  • Both Python and JavaScript use the import keyword to import modules.
  • Python module members do not need to be explicitly exported using the export keyword, as all members in a module are automatically exported.
Private members in Python modules

In Python, all variables, functions, classes, and other members that can be accessed within the module are automatically exported and can be freely imported by other modules. Python does not provide a way to protect module private members. To indicate that certain members in a module should not be imported by other modules, it is common convention to prefix the member names with _ or __. Additionally, when using third-party modules, it is important to follow this convention and avoid directly importing and using members that start with an underscore.

Difference Quick View

FeatureJavaScriptPython
Import moduleimport foo from 'module'import module
Import specific members from moduleimport { foo, bar } from 'module'from module import foo, bar
Import module with aliasimport { foo as alias } from 'module'from module import foo as alias
Import entire module with aliasimport * as alias from 'module'import module as alias
Default exportexport default module-
Named exportexport foo-
tip

In Python, the module name is determined by the file name. The file name cannot contain - or it will not be able to be imported by other modules.

Python follows certain conventions when converting between file names and module names:

File NameModule NameExplanation
my_module.pymy_moduleModule name is the same as the file name
myModule.pymymoduleUppercase letters are automatically converted to lowercase

Resources