# Of Syntax Warnings and Symbol Tables

DevFeed: [Of Syntax Warnings and Symbol Tables](<https://devfeed.tech/articles/of-syntax-warnings-and-symbol-tables-29441.md>)

Original publisher: [Read original article](<http://akaptur.github.com/blog/2014/06/11/of-syntax-warnings-and-symbol-tables/>)

Published: 2014-06-12T01:30:00Z

Content type: article

Language: en

Sources: [Allison Kaptur](<https://devfeed.tech/sources/allison-kaptur.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [import](<https://devfeed.tech/topics/import.md>), [syntax](<https://devfeed.tech/topics/syntax.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [bug](<https://devfeed.tech/tags/bug.md>), [code](<https://devfeed.tech/tags/code.md>), [import](<https://devfeed.tech/tags/import.md>), [python](<https://devfeed.tech/tags/python.md>), [syntax](<https://devfeed.tech/tags/syntax.md>)

## AI overview

The article examines Python's SyntaxWarning for star-imports outside module scope. It explains that the warning can occur without an immediate syntax error, while free variables in a function or nested block can cause the compiler to raise a SyntaxError.

## Source excerpt

A Hacker Schooler hit an interesting bug today: her program would sometimes emit the message SyntaxWarning: import * only allowed at module level. I had never seen a SyntaxWarning before, so I decided to dig in. The wording of the warning is strange: it says that star-import is only allowed at the module level, but it's not a syntax error, just a warning. In fact, you can use a star-import in a scope that isn't a module (in Python 2): 1 2 3 4 5 6 7 >>> def nope(): ... from random import * ... print randint(1,10) ... <stdin>:1: SyntaxWarning: import * only allowed at module level >>> nope() 7 The Python spec gives some more details: The from form with * may only occur in a module scope. If the wild card form of import -- import * -- is used in a function and the function contains or is a nested block with free variables, the compiler will raise a SyntaxError. Just having import * in a function isn't enough to raise a syntax error - we also need free variables. The Python execution model refers to three kinds of variables, 'local,' 'global,' and 'free', defined as follows: If a name is bound in a block, it is a local variable of that block. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a free variable. Now we can see how to trigger a syntax error from our syntax warning: 1 2 3 4 5 6 7 8 9 >>> def one(): ... def two(): ... from random import * ... print randint(1,10) ... two() ... <stdin>:2: SyntaxWarning: import * only allowed at module level File "<stdin>", line 3 SyntaxError: import * is not allowed in function 'two' because it is a nested function and similarly, 1 2 3 4 5 6 7 8 9 >>> def one(): ... from random import * ... def two(): ... print randint(1,10) ... two() ... <stdin>:1: SyntaxWarning: import * only allowed at module level File "<stdin>", line 2 SyntaxError: import * is not allowed in function 'one' because it