Skip to content

Mojo Error: use of unknown declaration 'x'

error: use of unknown declaration 'x' means Mojo found a name it has never heard of. It’s the most common error in the first hour of Mojo, and the fix is almost always one line: fix a typo, add an import, or move a variable. This page shows how to tell which one you need. For the other errors in this series, see Mojo Errors Explained.

Here’s a small program with a typo in it:

def main():
var name = "Ada"
print(nmae)
greet.mojo:3:11: error: use of unknown declaration 'nmae'; did you mean 'name'?
print(nmae)
^~~~
name
mojo: error: failed to parse the provided Mojo source module

Read it from left to right:

  1. greet.mojo:3:11 is the file, the line (3) and the column (11) where Mojo got stuck.
  2. use of unknown declaration 'nmae' says which name it doesn’t know.
  3. did you mean 'name'? is a guess. When a known name looks close, Mojo prints it, and the line under the ^~~~ marks shows the word it would put there instead.
  4. The last line, failed to parse the provided Mojo source module, only says the program couldn’t be built. The useful part is always the first error: line.

A declaration is the place where a name is created: var name = ... creates a variable, def greet() creates a function, struct Point creates a type, and from std.math import sqrt brings a name in from a library. “Unknown declaration” means Mojo looked for the place where this name was created and didn’t find one, at least not anywhere it’s allowed to look from this line.

Mojo checks every name before the program runs. In Python, a typo like this only blows up with a NameError when that line actually runs, which might be days later. In Mojo, the program doesn’t build until every name is known.

If the message ends with did you mean '...'?, the suggestion is usually right. Change the name to the one Mojo suggests:

def main():
var name = "Ada"
print(name)

Output:

Ada

Remember that names are case sensitive: Print, TRUE and Name are all different from print, True and name.

Some things live in the standard library and need an import first. print, len, Int, String, List and Dict are always there, but sqrt isn’t. For common names, Mojo tells you the exact line to add:

def main():
print(sqrt(16.0))
roots.mojo:2:11: error: use of unknown declaration 'sqrt'; did you mean to import it from 'std.math'? Add 'from std.math import sqrt'
print(sqrt(16.0))
^~~~
mojo: error: failed to parse the provided Mojo source module

Copy that line to the top of the file:

from std.math import sqrt
def main():
print(sqrt(16.0))

Output:

4.0

Standard library imports start with std. in Mojo 1.2: from std.math import sqrt, from std.collections import Set, from std.sys import stderr. If you write math.sqrt(...) without any import, the unknown name is math itself. Either import the function as above, or import the module with from std import math and then write math.sqrt(16.0). The standard library docs list which module each name lives in.

Fix 3: Use the Mojo name, not the Python one

Section titled “Fix 3: Use the Mojo name, not the Python one”

Many Python names don’t exist in Mojo. The error doesn’t always guess well here; for str it suggests std, which isn’t what you want:

def main():
var s = str(5)
print(s)
convert.mojo:2:13: error: use of unknown declaration 'str'; did you mean 'std'?
var s = str(5)
^~~
std
mojo: error: failed to parse the provided Mojo source module

Use the Mojo type names instead:

def main():
var s = String(5)
var on = True
print(s, on)

Output:

5 True
Python habit Mojo Notes
str(5) String(5)
int("5") Int("5") It can fail, so it needs try or raises
float(n) Float64(n)
true, TRUE True Same as Python, only True works
import math from std import math Or import the function itself

The Mojo Type Conversion lesson covers String(...), Int(...) and Float64(...) in detail.

Fix 4: Create the variable outside the block

Section titled “Fix 4: Create the variable outside the block”

In Mojo, a variable made with var inside an if, a loop or any other indented block only exists inside that block. Python lets you use it afterwards; Mojo doesn’t:

def main():
var score = 7
if score > 5:
var grade = "pass"
print(grade)
grade.mojo:5:11: error: use of unknown declaration 'grade'
print(grade)
^~~~~
mojo: error: failed to parse the provided Mojo source module

There’s no “did you mean” here, because the name is spelled right. It just isn’t visible on line 5. Create the variable before the block, with a starting value, and only change it inside:

def main():
var score = 7
var grade = "fail"
if score > 5:
grade = "pass"
print(grade)

Output:

pass

The same goes for loop variables: after for i in range(3):, the name i is gone. See Mojo Scope for the full rules.

  • A typo when you change a variable. totl += n gives this error with did you mean 'total'?. But totl = n (a plain =) gives a different one, implicit declaration of ‘totl’ is not allowed, because Mojo thinks you’re trying to create a new variable.
  • A field inside a method without self.. Mojo has its own message for that one: cannot access instance field 'count' directly; did you mean 'self.'?. Write self.count. See Mojo Methods.
  • Code copied from Python. str, int, true, math.sqrt without an import, and random.random() without from std.random import ... all end here.
  • A variable from another function. Each function has its own variables. Pass the value in as an argument, as shown in Mojo Function Arguments.

This program has two unknown declarations. Fix both so it prints the average.

def main():
var nums: List[Int] = [4, 8, 6]
for n in nums:
var total = 0
total += n
print(Float64(totl) / Float64(len(nums)))

Hint: one name is a typo. The other one is created in the wrong place.

Show solution
def main():
var nums: List[Int] = [4, 8, 6]
var total = 0
for n in nums:
total += n
print(Float64(total) / Float64(len(nums)))

Output:

6.0

totl was a typo for total. And total was created inside the loop, so it didn’t exist after it (it would also have started again at 0 on every pass). Moving var total = 0 above the loop fixes both.

  • The error means Mojo can’t find where a name was created, at least not from this line.
  • If it says did you mean '...', check the spelling first.
  • For library names, add the import it suggests, like from std.math import sqrt.
  • Python names such as str, int and true don’t exist; use String, Int and True.
  • A var made inside a block is gone after the block. Create it before the block.

Next up: implicit declaration of ‘x’ is not allowed, the error you get when you forget var.

Quick check

1. You write print(sqrt(2.0)) and get "use of unknown declaration 'sqrt'". What is the fix?
2. A variable is made with var inside an if block and printed after it. What happens?
3. Which line builds in Mojo?