Python Modules (module name given before assignment)

I have it

in the same directory as main()

script (

). As

I have

import vm
x = vm.Something()


Then python complains

UnboundLocalError: local variable 'vm' referenced before assignment


Why? There were no errors on import.


I found that if I did

from vm import * 


Instead, it worked. Also for another file / module I created a simple one works import

. I have uploaded the complete code to the GitHub Gist


source to share

1 answer

Inside your function main

, you had a string vm = VirtualMemory(args['numFrames'], algo)

. The upshot of this is that Python recognizes vm

as a local variable within a function, and so when you try to access vm

what a module means vm

before assigning a value to it locally, it complains that you didn't assign a value to it.

It renames any variable vm

or your module vm

to something else.

( from X import *

One last thing : avoid operators , they debug heavily, list what you import explicitly. You don't want to import type names anyway main




All Articles