views:

370

answers:

2

I've got a Django product I'm using iPython to interact with.

I'm trying to have modules automatically loaded when I start a shell:

python manage.py shell

I've copied .ipython/ipythonrc to the root directory of the project and added to the file:

import_some module_name model1 model2

However, when I start the shell, these names are not being loaded.

What am I doing wrong?

+4  A: 

I don't know about ipythonrc, but if you only need the models, you could use django-extensions. After you install it, you've got a plethora of new managment commands, including shell_plus, which will open a ipython session and autoload all your models:

python manage.py shell_plus
piquadrat
A: 

BryanWheelock Your solution won't work because your shell is the result of the spawn not a direct interatction with it. What you want to do is this - or at least this is what I do.

Within your workspace (the place where you type python manage.py shell) create a ipythonrc file. In it put the following:

include ~/.ipython/ipythonrc

execute from django.contrib.auth.models import User
# .
# .
# .
execute import_some module_name model1 model2

For example I also add the following lines in mine..

# Setup Logging
execute import sys
execute import logging
execute loglevel = logging.DEBUG
execute logging.basicConfig(format="%(levelname)-8s %(asctime)s %(name)s %(message)s", datefmt='%m/%d/%y %H:%M:%S', stream=sys.stdout )
execute log = logging.getLogger("")
execute log.setLevel(loglevel)
execute log.debug("Logging has been initialized from ipythonrc")
execute log.debug("Root Logger has been established - use \"log.LEVEL(MSG)\"")
execute log.setLevel(loglevel)
execute log.debug("log.setlevel(logging.DEBUG)")
execute print ""

This allows you to use logging in your modules and keep it DRY. Hope this helps.

rh0dium