JS
PY

JavaScript to Python

10 lessons

Progress0%
1Variables and Constants2Functions3Arrays vs Lists4Objects vs Dictionaries5Classes and OOP6Modules and Imports7Array Methods vs Comprehensions8Error Handling9Async Programming10File I/O
All Mirror Courses
JS
PY
Modules and Imports
MirrorLesson 6 of 10
Lesson 6

Modules and Imports

Organizing code across files

Introduction

In this lesson, you'll learn about modules and imports in Python. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with organizing code across files.

PY
In Python:

Python has its own approach to organizing code across files, which we'll explore step by step.

The Python Way

Let's see how Python handles this concept. Here's a typical example:

PY
Python Example
# math_utils.py
PI = 3.14159
def add(a, b): return a + b
class Calculator: pass   # ...

# main.py
from math_utils import PI, add
import math_utils
from math_utils import Calculator as Calc

# Import everything (discouraged)
from math_utils import *

# Standard library
import os
import json
from pathlib import Path

# Dynamic import
import importlib
mod = importlib.import_module("math_utils")

Comparing to JavaScript

Here's how you might have written similar code in JavaScript:

JS
JavaScript (What you know)
// math.js
export const PI = 3.14159;
export function add(a, b) { return a + b; }
export default class Calculator { /* ... */ }

// main.js
import Calculator, { PI, add } from "./math.js";
import * as MathLib from "./math.js";

// Dynamic import
const { add: sum } = await import("./math.js");

// CommonJS (Node.js)
const fs = require("fs");
module.exports = { add };
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

Python has no export keyword — everything is importable

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

from module import name ≈ import { name } from 'module'

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

import module ≈ import * as module

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

Packages need an __init__.py file

Step-by-Step Breakdown

1. Named Imports

In Python, every top-level name in a module is automatically public. You import specific names with from.

JS
JavaScript
import { PI, add } from "./math.js";
PY
Python
from math_utils import PI, add

2. Namespace Import

import module loads the whole module under a namespace.

JS
JavaScript
import * as MathLib from "./math.js";
MathLib.add(1,2);
PY
Python
import math_utils
math_utils.add(1, 2)
Common Pitfall
from module import * pollutes the namespace and can cause name collisions. Avoid it.

3. Packages

A folder becomes a Python package when it contains an __init__.py file.

JS
JavaScript
// No equivalent — Node uses package.json
PY
Python
# mypackage/__init__.py (can be empty)
from mypackage.utils import helper
Rule of Thumb
Use __init__.py to control what gets exported when someone writes from mypackage import *.

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • Python has no export keyword — everything is importable
  • from module import name ≈ import { name } from 'module'
  • import module ≈ import * as module
Common Pitfall
Don't assume Python works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • No export keyword — everything is public by default
  • from module import name for named imports
  • Packages = folders with __init__.py
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Python to practice these concepts.
PreviousNext