21
(myvenv)me:src orokusaki$ python manage.py shell -i ipython
Python 2.7.2 (default, Jun 16 2012, 12:38:40) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> while True:
...     pass  # :(
...

I have IPython installed globally, and it works while this myvenv virtualenv is activated just fine. However, if I start the Django shell (with or without -i), it won't use IPython. I've never had this problem before.

Note: django-admin.py vs manage.py won't make a difference.

4
  • 3
    just for sake of experiment - have you tried installing ipython into that virtualenv as well? Commented Jan 31, 2013 at 1:17
  • @MikeRepass - worked :) thanks (put it into an answer) - Funny thing, if I install iPython only in my virtualenv, it doesn't use my global readline install (seems counter intuitive), but since I have iPython installed globally, and now in the virtualenv, which ipython points to the virtualenv's version, but readline somehow works... so I wasn't even thinking about installing it into my virtualenv, until you suggested it. thanks again. Commented Jan 31, 2013 at 1:26
  • 1
    If IPython is not installed in the env, it may 'work', but your env will be broken because the expected python entry point will not be used. If you want it to work this way (obviously only for envs with --system-site-packages), edit the first line of the ipython script to use /usr/bin/env python instead of the hardcoded path to the Python which installed IPython. Commented Jan 31, 2013 at 4:49
  • As for readline, I don't suppose you installed readline with pip, did you? Because that doesn't actually install a readline that will be importable from any environment other than a running IPython session (IPython has a specific hack, because too many people make the mistake of installing readline with pip). You must use easy_install -a readline. Even after thatt, you will need virtualenv ≥ 1.8.3 in order to stage readline properly into envs, otherwise virtualenv will copy the System readline, and not your readline into the env. Commented Jan 31, 2013 at 4:53

3 Answers 3

48

Try installing it into virtualenv! :-)

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

3 Comments

Is there a way (hack/workaround) to use the system-wide IPython?
Yes @minrk told how to use a virtualenv with system-wide components here.
Extending @blueyed comment: Is there a way to use the system-wide iPython (because I don't want to install it in every Env) but not use system-site-packages (because I running in an Env using the default --no-site-packages)?
16

I love iPython but don't like installing it in all my virtualenvs, and I've found a good solution to allow for that. Instead of using python manage.py shell, you can just use the system iPython directly.

In order for this to work properly, you need to set the DJANGO_SETTINGS_MODULE so that it corresponds to your project.

export DJANGO_SETTINGS_MODULE=yourproject.settings

If this is your only Django project, the easiest solution is to add that line to your .bashrc.

If you have several Django projects and want to avoid having to change the variable every time you switch between projects, you can add that export line above tailored to each project to the postactivate scripts of all your Django virtualenvs. For me, the postactivate script is at ~/.virtualenvs/myvenv/bin/postactivate.

2 Comments

+1 for an alternative solution, but I've left the other as the correct answer, only because your solution is a bit of a hack.
if you do this make sure you unset the DJANGO_SETTINGS_MODULE so in ~/.virtualenvs/postdeactivate put unset DJANGO_SETTINGS_MODULE only needs to be done once to cover all virtualenvs
2

In my case, I really want to run IPython in multiple virtual environments (created using the default --no-site-packages) each used for a different Django project. I did not want to install IPython in each env.

As @Arash mentioned, exporting the DJANGO_SETTINGS_MODULE works but it's difficult to manage it when you have multiple projects.

I finally solved this issue in two parts.

First I added the directories "/usr/lib/python2.7/dist-packages" and "/usr/lib/pymodules/python2.7"to sys.path in the /usr/bin/ipython script as mentioned here.

Next, I use a manage_to_ipython.py script to create a django-project-specific script called runipython.py leveraging the manage.py file created by django-admin.py

Once in each Django project, $cd <project_dir> then run python manage_to_ipython.py

Now, to run IPython simply type ./runipython.py

The modified /usr/bin/ipython and the manage_to_ipython.py scripts can be found here.

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.