Modules and Code Organization

15.1 Importing

import std/strutils                     # stdlib module (std/ prefix recommended)
import std/[os, times, tables]          # multiple
import mymodule                         # local file mymodule.nim
import ./utils/helpers                  # relative path
import pkg/jsony                        # a nimble package (pkg/ prefix optional)
from std/math import sqrt, PI           # selective import
import std/strutils except split        # exclude
import std/strutils as su               # alias
include partial                         # textual inclusion (rare)

15.2 Exporting

# geometry.nim
type Vec* = object
  x*, y*: float
proc len*(v: Vec): float = ...
proc helper(v: Vec) = ...               # private (no *)
export someOtherModule                  # re-export

15.3 Module Initialization and Main Guard

Top-level statements run at program start in import order.

when isMainModule:
  echo "Run directly, not imported"
  runTests()

15.4 Project Structure

myproject/
├── myproject.nimble
├── src/
│   ├── myproject.nim          # main module / library entry
│   └── myproject/
│       ├── core.nim
│       └── utils.nim
├── tests/
│   ├── config.nims
│   └── test_core.nim
└── docs/

15.5 Circular Imports

Not supported directly. Restructure into a common types module, or use forward declarations and include tricks. (Nim's upcoming IC/"Nimony" plans address this.)