Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'reload' is not defined

I'm using python 3.2.2. When I write a simple program, I meet the problem.

>>> reload(recommendations) Traceback (most recent call last):   File "<pyshell#6>", line 1, in <module>     reload(recommendations) NameError: name 'reload' is not defined 

How should I do it?

like image 602
MindHacks Avatar asked Apr 13 '12 14:04

MindHacks


People also ask

How do I fix NameError is not defined in Python?

The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.

How do I reload a Python module?

The reload() - reloads a previously imported module or loaded module. This comes handy in a situation where you repeatedly run a test script during an interactive session, it always uses the first version of the modules we are developing, even we have mades changes to the code.


2 Answers

An update to @Gareth Latty's answer. imp was depreciated in Python 3.4. Now you want importlib.reload().

from importlib import reload 
like image 45
James Hirschorn Avatar answered Sep 29 '22 01:09

James Hirschorn


You probably wanted importlib.reload().

from importlib import reload 

In Python 2.x, this was a builtin, but in 3.x, it's in the importlib module.

Note that using reload() outside of the interpreter is generally unnecessary, what were you trying to do here?

like image 80
Gareth Latty Avatar answered Sep 29 '22 01:09

Gareth Latty