Alias import

2 snippets across 2 stacks - Python, TypeScript

Also written as import alias

PYPython

Basic Imports

PY · Modules & Imports
Syntax
import module
from module import name
import module as alias
Example
import json
from pathlib import Path
from collections import defaultdict, Counter
import 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.

TSTypeScript

paths & baseUrl

TS · Configuration
Syntax
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@alias/*": ["src/folder/*"]
    }
  }
}
Example
// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@components/*": ["src/components/*"],
      "@utils/*": ["src/utils/*"],
      "@api/*": ["src/api/*"],
      "@types/*": ["src/types/*"]
    }
  }
}

// In your code:
import { Button } from "@components/Button";
import { formatDate } from "@utils/date";
import type { User } from "@types/user";
Output
// Path aliases shorten deep relative imports like ../../../../utils

Note paths only affects TypeScript's type resolution - it does NOT rewrite imports in emitted JS. Your bundler (Vite, webpack) or runtime (ts-node, tsx) needs matching alias config. For Node.js, package.json "imports" with subpath patterns is the modern alternative that works without bundler config.

Frequently asked questions

How does Python handle alias import?
This task is covered in 2 stacks on this page: Python, TypeScript. The "Basic Imports" snippet in Python uses `import module`.
Which code does the Python example use?
The "Basic Imports" snippet uses `import module`, from the Modules & Imports section of the Python cheat sheet.
Which stacks cover "alias import" on this page?
Python, TypeScript. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Basic Imports": Convention: standard library imports first, then third-party, then local. Separate groups with a blank line. Use isort to auto-format.