automatically import lots of modules at python manage.py shell time?

262 views
Skip to first unread message

Phlip

unread,
Mar 26, 2010, 5:47:03 PM3/26/10
to Django users
Djangoists:

I am in anguish over watching my colleagues fire up python manage.py
shell...

...and then typing several incredibly long import lines before
actually getting anything done.

These same colleagues also seem to have itchy ^C fingers, meaning they
bail out of the shell too often, too.

How can we fix the first problem? How can this (otherwise useful)
shell use a minimal or invisible import rubric, to grab, say, a bunch
of models so they are all ready & available for use?

--
Phlip
http://penbird.deviantart.com/

Tom X. Tobin

unread,
Mar 26, 2010, 6:03:33 PM3/26/10
to django...@googlegroups.com

I use something like this (assuming you have iPython installed):

**********
#!/usr/bin/env ipython -i -nobanner

# don't need this part if DJANGO_SETTINGS_MODULE is already set in
your environment
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'YOUR.SETTINGS.MODULE'

from django.db.models import Count, Max, Min, Q
from django.db.models.loading import cache as appcache

local_dict = locals()
for model_class in appcache.get_models():
local_dict[model_class.__name__] = model_class
del local_dict, appcache, os
**********

Save that to a file in your PATH, make it executable, and you should be set.

Phlip

unread,
Mar 26, 2010, 6:45:42 PM3/26/10
to Django users
Tom X. Tobin wrote:

> #!/usr/bin/env ipython -i -nobanner


> Save that to a file in your PATH, make it executable, and you should be set.

Awesome--thanks. And, instead of chmod +x-ing it, I added it to our
common fab files, so everyone gets it with 'fab shell'.

And I could not get ip = IPython.ipapi.get() working (not sure why -
it returned a None), so I added it with this contemptible but
bulletproof hack:

def shell():
open('.ipython', 'w').write('''


# don't need this part if DJANGO_SETTINGS_MODULE is already set in
your environment
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'dev2_settings' # TODO look
up which one

from django.db.models import Count, Max, Min, Q
from django.db.models.loading import cache as appcache

local_dict = locals()
for model_class in appcache.get_models():
local_dict[model_class.__name__] = model_class
del local_dict, appcache, os

''')
os.system('ipython -i -nobanner .ipython')

--
Phlip
http://c2.com/cgi/wiki?ZeekLand

Tom X. Tobin

unread,
Mar 26, 2010, 6:52:00 PM3/26/10
to django...@googlegroups.com
On Fri, Mar 26, 2010 at 5:45 PM, Phlip <phli...@gmail.com> wrote:
> os.environ['DJANGO_SETTINGS_MODULE'] = 'dev2_settings'  #  TODO  look
> up which one

My local copy actually has the following code in it:

********************
import os
import re
import sys

m = re.search(r'^([a-zA-Z0-9_]+)-shell$', os.path.basename(sys.argv[0]))
mn = 'avclub'
if m:
g = m.group(1)
if g in ['avclub', 'onion']:
mn = g
del g
del m
os.environ['DJANGO_SETTINGS_MODULE'] = 'afns.sites.local_%s.settings'
% (mn,) # "afns" is the Onion's Python package
********************

This way I can just make symlinks named "avclub-shell" and
"onion-shell" to the original executable, and it will use the
respective settings files automatically without having to maintain
separate copies. You could also get fancier with optparse, I suppose.

Bjunix

unread,
Mar 26, 2010, 8:09:46 PM3/26/10
to Django users
django-extensions[1] brings the shell_plus command which auto-imports
all models in installed apps. You can use this or have a look at it's
source.

[1] http://github.com/django-extensions/django-extensions

On Mar 26, 11:52 pm, "Tom X. Tobin" <tomxto...@tomxtobin.com> wrote:

Rolando Espinoza La Fuente

unread,
Mar 27, 2010, 1:35:58 PM3/27/10
to django...@googlegroups.com
On Fri, Mar 26, 2010 at 7:39 PM, Bjunix <bju...@bjunix.de> wrote:
> django-extensions[1] brings the shell_plus command which auto-imports
> all models in installed apps. You can use this or have a look at it's
> source.

Yes. shell_plus is quite handy.

$ bin/manage.py shell_plus
[...]
From 'api' autoload: RequestLog
From 'lostiempos' autoload: Section, Subsection, Ad, HtmlArchive,
SearchHistory, SearchHistoryCount
From 'south' autoload: MigrationHistory

Imports all your models by default. Also with iPython you can write
"from" then press [Up]
and will show you the entries starting with "from" in the history.

Regards,

Rolando

Matt Schinckel

unread,
Mar 28, 2010, 3:52:23 AM3/28/10
to Django users
On Mar 28, 3:35 am, Rolando Espinoza La Fuente <dark...@gmail.com>
wrote:

I wrote a similar app called django-bshell which does the same, but
uses bpython as the shell.

You can find this on pypi, and it adds a 'bshell' manage command.

Matt.

Reply all
Reply to author
Forward
0 new messages