Use the import statement to bring in other Olive files. Dots in the module name map to directory separators:
import math
import utilities.network
import physics.gravity as gravity
let x = math.sqrt(16)
let g = gravity.GBy default, import math looks for math.liv in the same directory as the current file.
Olive separates imported modules from executable main scripts by placing strict restrictions on top-level execution:
- Entry Point Only: Only the file executed directly by the
pit runcommand is treated as the "main" script. The compiler automatically executes its top-level statements and calls itsmain()function (if defined). - Imported Modules: When a module is imported by another file, its top-level statements (such as prints, loops, or direct function calls) are not executed. The compiler only processes variable declarations, constants, structures, and function definitions.
- Safe, Side-Effect-Free Imports: This design ensures that importing a file never produces unexpected side effects (like initiating network calls, print outputs, or database connections). It makes modules completely safe, predictable, and modular.
If you only need specific names from a module, use from ... import:
from math import sqrt, pi
from data.processing import clean_string as clean, parse_json as parse
print(sqrt(pi))
let data = parse(clean(raw_input))If a library is written in another language (like C or Rust), it can be used in Olive by defining its interface through a native import.
import "physics.so" as physics
let result = physics.compute_gravity(10.0, 5.0)The compiler manages symbol resolution and calling convention compliance.
Olive uses a naming convention for visibility:
- Public: Any name that doesn't start with an underscore. Accessible from other modules.
- Private: Names starting with
_are private to the module where they're defined. The compiler enforces this.
// In utils.liv
fn _secret():
pass
// In main.liv
import utils
// utils._secret() // Error: cannot access private member `_secret`A typical project layout:
my_project/
├── main.liv
├── models.liv
└── utils/
├── __init__.liv (optional)
└── network.liv
In main.liv:
import models
import utils.networkThe standard library is resolved dynamically at runtime and exposed via built-in namespaces.
import mathConstants
math.PI // 3.141592653589793
math.E // 2.718281828459045
math.TAU // 6.283185307179586
math.INF // 1.0e308Trigonometry (all angles in radians)
math.sin(x) math.asin(x)
math.cos(x) math.acos(x)
math.tan(x) math.atan(x)
math.atan2(y, x)
math.degrees(x) // radians -> degrees
math.radians(x) // degrees -> radiansExponential and logarithm
math.exp(x) // e^x
math.log(x) // natural log
math.log10(x) // log base 10
math.pow(b, e) // b^e (floats)
math.ipow(b, e) // b^e (integers)Roots and rounding
math.sqrt(x)
math.cbrt(x)
math.hypot(x, y) // sqrt(x² + y²)
math.floor(x) // -> int
math.ceil(x) // -> int
math.round(x) // -> int
math.abs(x)
math.clamp(x, lo, hi)
math.fmod(x, y)
math.copysign(x, y)Hyperbolic
math.sinh(x) math.asinh(x)
math.cosh(x) math.acosh(x)
math.tanh(x) math.atanh(x)Number theory
math.gcd(a, b)
math.lcm(a, b)
math.factorial(n)
math.comb(n, k) // n choose k
math.perm(n, k) // n permute kUtilities
math.min(a, b)
math.max(a, b)
math.isclose(a, b) // abs(a - b) < 1e-9
math.erf(x)Synchronous file operations:
import io
let contents = io.file_read("data.txt")
io.file_write("output.txt", "hello")Asynchronous file operations that return futures. Must be used with await:
import aio
async fn read_file():
let contents = await aio.async_file_read("data.txt")
await aio.async_file_write("output.txt", contents)Low-level TCP networking:
import net
let stream = net.tcp_connect("127.0.0.1:8080")
net.tcp_send(stream, "GET / HTTP/1.0\r\n\r\n")
let response = net.tcp_recv(stream, 4096)
net.tcp_close(stream)Simple HTTP client:
import http
let body = http.http_get("https://example.com/api/data")
let resp = http.http_post("https://example.com/api/submit", "{\"key\": \"value\"}")Random number generation:
import random
random.random_seed(42)
let f = random.random_get() // float in [0.0, 1.0)
let n = random.random_int(1, 100) // int in [1, 100]