10
$ python
>>> import myapp
>>> reload(myapp)
<module 'myapp' from 'myapp.pyc'>
>>>

ctrl+D

$ python
>>> from myapp import *
>>> reload(myapp)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'myapp' is not defined

Why this behaves differently? How can I reload when using from myapp import *?

6
  • The reason I use reload() is because I change the source code of the module and test it and the reason I use from myapp import is because it saves me typing. Commented May 1, 2012 at 17:32
  • 1
    reload is to be used with a lot of caution. It behaves unexpected in many ways, the worst of which is that classes exist in many versions inside the VM, depending on whether you have old instances of reloaded classes hanging around. reload was removed from Python 3 for that reason. Often it's much easier to set up a small script setting up all the modules you need and then drop into a shell using import code; code.interact(local=locals()). The Python interpreter starts up really fast, so this is usually even faster then searching for reload in the readline history. Commented May 1, 2012 at 18:31
  • @NiklasB. Thank you for advice, but I can't imagine this without some example. If it's possible and you have time to write more about it, please put some example to an answer. Or I can create a new question based on your comment, if you're not against it. Commented May 1, 2012 at 19:02
  • 2
    I meant something like pastie.org/3844991. Instead of having a shell open and trying to reload stuff (which can't properly work), you can just reload the whole shell from scratch using that script. This also has the advantage of forcing you to explicitly setup the context you want to test, so you can reproduce everything you do and have the option to turn this into a unit test easily later on. Commented May 1, 2012 at 19:04
  • @NiklasB The link to pastie is not working. Plz give me sm code sample on how may I implement code.interact(...) Method... Ur method is very tempting Commented Dec 7, 2015 at 7:52

4 Answers 4

19

From http://docs.python.org/library/functions.html#reload :

If a module imports objects from another module using from ... import ..., calling reload() for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to use import and qualified names (module.name) instead.

So, you should do something like:

from myapp import *
....
import myapp
reload(myapp)
from myapp import *
Sign up to request clarification or add additional context in comments.

Comments

3

How can I reload when using from myapp import *?

You can't. This is one of the reasons why using from X import * is a bad idea.

1 Comment

It's not a bad idea when debugging. It saves typing.
3

With from myapp import *, you don't have a reference to your module in a variable name, so you can't use a variable name to refer to the module.

Of course, there's nothing preventing you from importing it again to get the reference to the module in a name you can use. Since it's already been imported once, it won't actually be imported again:

import myapp
reload(myapp)

You can also get the reference directly from sys.modules.

import sys
reload(sys.modules["myapp]")

2 Comments

Importing and reloading the module won't actually re-bind any names that were bound with from foo import *. This may or many not make the reload useless.
The reason I use reload() is because I change the source code of the module and test it and the reason I use from myapp import is because it saves me typing.
1

To clarify Wooble's comment, using "from foo import *" brings everything from foo into the current namespace. This can lead to name collisions (where you unintentionally assign a new value to a name already in use) and can also make it harder to tell where something came from. While a few libraries are often used this way, it generally causes more problems than it is worth.

Also, since it has been brought into the current namespace it cannot simply be reloaded. It is generally better to keep it in a separate namespace (perhaps with a shorter convenience alias like just m). This allows you to reload (which is useful for testing, but rarely a good idea outside of testing) and helps keept he namespace pure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.