Jim
Jim

Reputation: 14280

How do you reload and autoreload using IPython?

I just installed IPython 0.13.1 and am having two problems. I have a small 'demo' project that contains an application called 'app':

.
├── app
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   └── views.py
├── demo
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   └── wsgi.py
└── manage.py

models.py contains:

from django.db import models

class Customer(models.Model):
    fname = models.CharField(max_length=25)

My first problem is reloading the models.py file after I make a change. If I open IPython, import my Customer class, and try to reload the models module, I get this error:

In [1]: from app.models import Customer
In [2]: reload(app.models)
--------------------------------------------------
NameError                    Traceback(most recent call last)
/Users/me/dotfiles/.virtualenvs/demo/lib/.../shell.pyc in <module>()
----> 1 reload(app.models)

NameError: name 'app' is not defined

While I'm new to Python, I do believe that 'models' is the module, and I it is in my INSTALLED_APPS settings. I also tried reload(app) and reload(models) with no success. What am I doing wrong?

My second problem is in using autoreload. After reading the doc page, I enabled it like this:

In [1]: %load_ext autoreload
In [2]: %autoreload 2

Now if I create a test file 'foo.py' with some function in it as the documentation illustrates, any changes I make to that function are reflected in IPython. But if I import my Customer class (as shown above) and add a second field "lname" to it and save the file, that change isn't reflected in IPython. If I run the command, "Customer??", the change doesn't show up. Also, if I run the aimport command, I see this:

In [5]: %load_ext autoreload
In [6]: %autoreload 2
In [7]: %aimport
Modules to reload:
all-except-skipped

Modules to skip:

What am I doing wrong? Is this the AppCache issue discussed here? I tried implementing the script shown but it throws errors when I run it. Thanks.

Upvotes: 3

Views: 2715

Answers (1)

rhettg
rhettg

Reputation: 2453

For your first issue, you can't reload(app.models) because you didn't add app.models to your namespace. You added only Customer. You can add import app.models to solve that.

I think your second problem is related. Since you imported the actual class into your namespace, reloading doesn't help. Presumably reloading just replaces the module in your namesspace, so if you refer to models.Customer rather than directly using Customer you should be in business.

Upvotes: 2

Related Questions