How do I add tab completion to the Python shell?

PythonShellInterpreter

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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.

Solution 3 - Python

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

Solution 4 - Python

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!!!

Solution 5 - Python

Fix for Windows 10 shell:

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

Solution 6 - Python

It looks like python3 has it out-of box!

Solution 7 - Python

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-

To solve this, I ran-

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.

Solution 8 - Python

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

Solution 9 - Python

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                 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionashchristopherView Question on Stackoverflow
Solution 1 - PythonashchristopherView Answer on Stackoverflow
Solution 2 - PythonPeter HoffmannView Answer on Stackoverflow
Solution 3 - PythonThomas WoutersView Answer on Stackoverflow
Solution 4 - Pythonalan_wangView Answer on Stackoverflow
Solution 5 - PythonMr.BView Answer on Stackoverflow
Solution 6 - PythonMichel SamiaView Answer on Stackoverflow
Solution 7 - PythonTrigonaMinimaView Answer on Stackoverflow
Solution 8 - PythonRuwinda FernandoView Answer on Stackoverflow
Solution 9 - PythonRuwinda FernandoView Answer on Stackoverflow