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.
The error message
Section titled “The error message”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) ^~~~ namemojo: error: failed to parse the provided Mojo source moduleRead it from left to right:
greet.mojo:3:11is the file, the line (3) and the column (11) where Mojo got stuck.use of unknown declaration 'nmae'says which name it doesn’t know.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.- 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 firsterror:line.
What it means
Section titled “What it means”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.
How to fix it
Section titled “How to fix it”Fix 1: Fix the spelling
Section titled “Fix 1: Fix the spelling”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:
AdaRemember that names are case sensitive: Print, TRUE and Name are all different from print, True and name.
Fix 2: Add the missing import
Section titled “Fix 2: Add the missing import”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 moduleCopy that line to the top of the file:
from std.math import sqrt
def main(): print(sqrt(16.0))Output:
4.0Standard 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) ^~~ stdmojo: error: failed to parse the provided Mojo source moduleUse 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 moduleThere’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:
passThe same goes for loop variables: after for i in range(3):, the name i is gone. See Mojo Scope for the full rules.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- A typo when you change a variable.
totl += ngives this error withdid you mean 'total'?. Buttotl = 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.'?. Writeself.count. See Mojo Methods. - Code copied from Python.
str,int,true,math.sqrtwithout an import, andrandom.random()withoutfrom 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.
Related errors
Section titled “Related errors”- implicit declaration of ‘x’ is not allowed: you assigned to a new name without
var. - expressions must not appear at file scope: your code is outside any function.
'X' value has no attribute 'Y': the value exists, but the method or field after the dot doesn’t.- All Mojo error pages: the full list.
Practice
Section titled “Practice”1. Two unknown names
Section titled “1. Two unknown names”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.0totl 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,intandtruedon’t exist; useString,IntandTrue. - A
varmade 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
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Variables in the Mojo manual: how variables are declared and where they can be used.
- Mojo tips for Python devs: the Python habits that don’t carry over to Mojo.
- Modules and packages: how imports work, for your own files and the standard library.
- Mojo standard library: every module, so you can find where a name lives.