0

I'd like to save myself some typing when I'm testing things in python manage.py shell. Basically I have a little shell script that gets me into the >>> shell, but I'd like to be able to automatically push in the import calls in my views.py script so I don't have to type them every time I want to test something. I tried writing them out in the shell script but it seems that they weren't passed to python shell.

Here's what I have so far.

#!/usr/local/bin/bash
python ~/path/to/manage.py shell

import datetime
from django.shortcuts import render_to_response
from mymodel.models import *
from myapp.forms import *
... and so on

What I'd like to happen is for all the import lines to be pushed into python shell so I don't have to type/copy-paste them everytime I want to debug at the shell.

2
  • 3 mins, 3 answers. Nice response rate for a Friday night. Commented Feb 23, 2013 at 2:55
  • Only the true geeks are out this late on a Friday :) Commented Feb 23, 2013 at 2:57

2 Answers 2

2

Put your prequel into a file. Here, I'll assume it's in a file appropriately named prequel. Then you can adjust your bash script like so:

cat prequel - | python ~/path/to/manage.py shell

cat concatenates files, but it can treat standard input as a file, so we're concatenating your prequel and then standard input and then piping that concatenation to the shell.

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

3 Comments

Nice. I didn't know you could use cat to do that.
I think this works. I say I think because the >>> doesn't show up, yet it seems when I type something, I am in fact in the shell.
@BrandonBertelsen: I'm guessing it won't print >>> unless it detects its standard input is a tty. If it's a pipe from cat (even if cat is getting its input from a tty), it's still a pipe and not a tty, so it won't print the >>>. I guess this is why that Django extension thing would be better. (It can execute stuff on its own without messing with standard input.)
1

There is also django-extensions: http://www.michelepasin.org/blog/2010/11/17/preloading-stuff-in-djangos-interactive-shell/

2 Comments

So, if I understand this correctly - you would just call python manage.py shell-plus.py?
That is just one of the command extensions. That particular command will autoload models. Check out: pythonhosted.org/django-extensions/command_extensions.html

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.