140

When starting a django application using python manage.py shell, I get an InteractiveConsole shell - I can use tab completion, etc.

Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

When just starting a python interpreter using python, it doesn't offer tab completion.

Can someone tell me what django is doing to give me an interactive console, or what I need to do to start an interactive console without a django app?

3
  • 1
    When you type in python, you get a >>> prompt, right? That's the interactive shell. What's your question?
    – S.Lott
    Oct 29, 2008 at 13:15
  • 5
    It doesn't offer tab completion. That is the problem. Oct 29, 2008 at 13:18
  • Please update your question to specify that. Oct 29, 2008 at 13:42

9 Answers 9

227

I may have found a way to do it.

Create a file .pythonrc

# ~/.pythonrc
# enable syntax completion
try:
    import readline
except ImportError:
    print("Module readline not available.")
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

then in your .bashrc file, add

export PYTHONSTARTUP=~/.pythonrc

That seems to work.

5
  • 2
    this works for Jython where IPython and bpython are not currently available. May 21, 2014 at 23:29
  • 1
    For some reason, the above worked for me in my old mac with "mountain lion" but not working for new mac with "el captan". I need to source ~/.bashrc everytime to make it work before starting python interpreter. Any tips?
    – hi15
    Oct 4, 2016 at 15:30
  • 1
    @hmi Try unifying your .bashrc and .bash_profile as suggested at the bottom of this page: joshstaiger.org/archives/2005/07/bash_profile_vs.html It also provides information on the difference between them. It might not solve the issue, but it might help.
    – Dangercrow
    Oct 21, 2016 at 9:11
  • @hmi Try putting it in /etc/bashrc, instead of ~/.bashrc.
    – voices
    Jan 18, 2018 at 5:22
  • Just for reference - I've probably come back to this answer more times than any other on StackOverflow, so thanks.
    – timmins
    Apr 16, 2019 at 20:31
35

I think django does something like https://docs.python.org/library/rlcompleter.html

If you want to have a really good interactive interpreter have a look at IPython.

0
27

For the record, this is covered in the tutorial: http://docs.python.org/tutorial/interactive.html

1
  • 8
    Link only answers are not encouraged, please expand your answer Apr 9, 2018 at 13:53
14

I use ptpython - it is a wonderful tool autocomplete shell cmd.

Installing ptpython is very easy, use pip tool

pip install ptpython

and for django shell, you should import the django env, like this

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testweb.settings")

Trust me, this is the best way for you!!!

0
8

Fix for Windows 10 shell:

pip install pyreadline3  # previously, pyreadline but that package was abandoned
pip install ipython
3

It looks like python3 has it out-of box!

1

In Python3 this feature is enabled by default. My system didn't have the module readline installed. I am on Manjaro. I didn't face this tab completion issue on other linux distributions (elementary, ubuntu, mint).

After pip installing the module, while importing, it was throwing the following error-

ImportError: libncursesw.so.5: cannot open shared object file: No such file or directory

To solve this, I ran-

cd /usr/lib ln -s libncursesw.so libncursesw.so.5

This resolved the import error. And, it also brought the tab completion in the python repl without any creation/changes of .pythonrc and .bashrc.

0

Yes. It's built in to 3.6.

fernanr@gnuruwi ~ $ python3.6
Python 3.6.3 (default, Apr 10 2019, 14:37:36)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.
Display all 318 possibilities? (y or n)
os.CLD_CONTINUED             os.O_RDONLY                  os.ST_NOEXEC                 os.environ                   os.getpid(                   os.readlink(                 os.spawnvpe(
os.CLD_DUMPED                os.O_RDWR                    os.ST_NOSUID                 os.environb                  os.getppid(                  os.readv(                    os.st
2
  • 1
    Hi what key did you press to get "Display all 318 possibilities? (y or n)". I tried 'tab' key it doesn't give me your auto-complete hint. I must be missing something simple
    – James H
    Mar 30, 2021 at 2:34
  • yes, tell how did it get autocomplete, which key or any setup needed, eager to know!
    – jdk
    May 6, 2022 at 4:28
-1

For older versions (2.x) above script works like charm :)

fernanr@crsatx4 ~ $ cat .bashrc | grep -i python
#Tab completion for python shell
export PYTHONSTARTUP=~/.pythonrc
fernanr@crsatx4 ~ $ . ~/.bashrc
fernanr@crsatx4 ~ $ echo $?
0
fernanr@crsatx4 ~ $ python2
Python 2.7.5 (default, Jun 11 2019, 14:33:56)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.
Display all 249 possibilities? (y or n)
os.EX_CANTCREAT             os.O_WRONLY                 
2
  • Your reply seems to be missing the content of "~/.pythonrc"
    – MadMike
    May 12, 2020 at 11:40
  • @MadMike I suspect it was intended to be shown below ashchristopher's answer ... not sure why it's an answer, though.
    – SamB
    Dec 31, 2020 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.