92

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?

6
  • 9
    A "simple program" probably doesn't need reload. Commented Apr 13, 2012 at 14:34
  • I want to use "reload(recommendations)","recommendations.abc()". But it can't find recommendations Commented Apr 14, 2012 at 15:54
  • 1
    @MindHacks If you haven't imported recommendations, you don't reload it, you import it. import recommendations. Why did you think you needed to use reload()? Commented Apr 14, 2012 at 17:42
  • @geoffspear Most "simple programs" reading files with non-latin characters do need reload, unfortunately. Commented Nov 14, 2017 at 13:47
  • @GennaroTedesco: if you're thinking of some awful thing you read that involves reload(sys), never do that. Just do with open('file', 'r', encoding='utf-8') as f: do_whatever. There is absolutely no need to reload() modules in simple programs, or really in any python program. Commented Nov 14, 2017 at 15:54

4 Answers 4

183

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?

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, it works. But if i wanna use the reload,i must import the file every time?
@MindHacks: What "file" are you talking about?
@MindHacks: imp is a module, not a file. (It's built into the interpreter core and does not correspond to any file.) And yes, to use imp.reload you have to import imp, but that isn't a big deal.
@MindHacks You need to import imp where you use reload(), yes. That's how namespacing works. Why would you want to reload a module outside of the interactive prompt anyway?
Thanks, but sigh. Why are they not trying to make the shell more useful?
|
14

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

from importlib import reload 

Comments

11

Try importlib.reload.

Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter.

from importlib import reload reload(module_name) 

Comments

1

As others have said, you need either importlib.reload(module) or at some earlier point you need to from importlib import reload. But you can hide the from importlib import reload in an initialization file. Make sure that PYTHONSTARTUP is defined in your shell. For example,

export PYTHONSTARTUP=$HOME/python/startup.py 

might a reasonable line to add to your ~/.bash_profile, if your shell is bash, and depending on where you store your python files. (If you’re following these instructions, start a new terminal window at this point so that the line is executed.) Then you can put the line

from importlib import reload 

in ~/python/startup.py and it will happen automatically. (Again, if you’re following along, start a new python session at this point.) This might look a bit complex just to solve this one problem, but it’s a thing you only have to do once, and then for every similar problem along the lines of “I wish python would always do this”, once you find the solution you can put it in ~/python/startup.py and forget about it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.