Skip to content

Modules And Imports

A program can be split across several .nlk files. The file you run is the entry file; it imports functions and constants from other files, which can import from further files. The import syntax is inspired by Python’s.

import_stmt := "import" module ("as" NAME)? ("," module ("as" NAME)?)*
| "from" module "import" names
| "from" module "import" "(" names ")"
names := NAME ("as" NAME)? ("," NAME ("as" NAME)?)* ","?
module := NAME ("." NAME)*

import, from, and as are keywords. Imports are top-level statements; they cannot appear inside a function, if, or loop.

main.nlk
from geometry import square
import geometry
print(square(3), geometry.square(2) + geometry.SIDES)
Run in playground
geometry.nlk
SIDES = 9
def square(x: num) -> num:
return x * x
Run in playground

import geometry makes the module’s names available as geometry.name. from geometry import square makes square available directly. as gives either form a different local name: import geometry as g or from geometry import square as sq.

A module name is a path relative to the folder of the file that contains the import. Each dot is a folder: import shapes.circle loads shapes/circle.nlk next to the importing file. A file in a folder resolves its own imports from that folder, so the same import can mean different files in different folders.

main.nlk
from shapes.circle import area
import shapes.square
print(area(2), shapes.square.perimeter(1.5))
Run in playground
shapes/circle.nlk
from numbers import pi
def area(radius: num) -> num:
return pi() * radius * radius
Run in playground
shapes/numbers.nlk
def pi() -> num:
return 3.14
Run in playground
shapes/square.nlk
def perimeter(side: num) -> num:
return 4 * side
Run in playground

Nilakan also ships built-in modules: plot and raylib. An import uses a built-in module only when no file of that name exists next to the importing file, so a local plot.nlk always wins.

Imports cannot reach a parent folder. Everything a program loads lives in the entry file’s folder or below it. from shapes import circle also works: when shapes.nlk has no name circle, the import loads the module shapes/circle.nlk instead.

A file that is imported may contain only imports, function definitions, and constants. A constant is a top-level assignment, including destructuring, and can use any expression, such as a call to one of the file’s functions. Each constant is assigned once.

Imported files load before the importing file’s own statements run. Each file loads once per run, in import order, after the files it imports; every run starts fresh. Because an imported file contains no statements that print, the order never changes a program’s output.

main.nlk
import report
Run in playground
report.nlk
TOTAL = 3
print(TOTAL)
Run in playground

Move statements like print(TOTAL) into the file you run, or into a function the entry file calls.

Each file has its own names. Two files can each define a helper with the same name, and an imported function always calls the helpers of the file that defines it. Diagnostics refer to a function from another file by its module name, such as geometry.square.

Reserved built-in names, such as len and print, cannot name functions or constants in imported files either.

Imports may appear anywhere at the top level. Top-level code can use an imported name only after its import. Function bodies can use imported names wherever the import appears in the file, as they can call functions defined later.

These are errors:

  • defining a function with the same name as an import, or assigning to an imported name
  • a parameter or local variable with the same name as an import
  • importing two different things under the same name; repeating an identical import is allowed
  • using a module as a value, as in x = geometry
  • reading an attribute of something that is not a module, as in xs.size

Functions keep their usual scope: they see their parameters and local variables. A function can call imported functions, including through a module name, but it cannot read imported constants. Pass the value in as an argument.

main.nlk
from config import LIMIT
def within(value: num) -> bool:
return value <= LIMIT
Run in playground
config.nlk
LIMIT = 10
Run in playground
main.nlk
from config import LIMIT
def within(value: num, limit: num) -> bool:
return value <= limit
print(within(4, LIMIT))
Run in playground
config.nlk
LIMIT = 10
Run in playground

The rules for printing functions also cross files. A function that prints, directly or through a function in another file, can only be called as a statement.

Files cannot import each other in a loop, directly or through other files. The error shows the chain of files. Move the shared definitions into a third file that both can import.

main.nlk
import a
Run in playground
a.nlk
import b
Run in playground
b.nlk
import a
Run in playground

The command line runs the file you name and reads its imports from disk. Imports in a program read from standard input resolve from the current directory. In the playground, Run runs the open file of My Draft.

Code Meaning
NLK2015 No file exists for an imported module.
NLK2016 An imported module has no definition with that name.
NLK2017 Imports form a cycle.
NLK2018 An imported file contains a statement other than a definition, or reassigns a constant.
NLK2019 A name clashes with an import, or is used before its import.
NLK2020 A module or function is used as a value, or an attribute is read from a value.
NLK5007 A program loads more than 100 files, or nests imports more than 32 files deep.