How do I add tab completion to the Python shell?

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?


Asked by: Arthur689 | Posted: 06-12-2021






Answer 1

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.

Answered by: Chester851 | Posted: 07-01-2022



Answer 2

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.

Answered by: Dominik896 | Posted: 07-01-2022



Answer 3

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

Answered by: Julian441 | Posted: 07-01-2022



Answer 4

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

Answered by: Dainton927 | Posted: 07-01-2022



Answer 5

Fix for Windows 10 shell:

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

Answered by: Audrey347 | Posted: 07-01-2022



Answer 6

It looks like python3 has it out-of box!

Answered by: Samantha808 | Posted: 07-01-2022



Answer 7

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.

Answered by: Brianna264 | Posted: 07-01-2022



Answer 8

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

Answered by: Daniel863 | Posted: 07-01-2022



Answer 9

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                 

Answered by: Chloe778 | Posted: 07-01-2022



Similar questions

python - Trick code completion with PyDev like with PDT?

Is there a way to help PyDev code completion by telling it the type of a variable? With PDT, you can use PHPDoc-like syntax for such purpose: /* @var $my_var MyClass */ $my_var = myFunction(); // PDT is able to figure out that $my_var is a MyClass object. But till now, I cannot figure out how to do the same in python.


python - File copy completion?

In Linux, how can we know if a file has completed copying before reading it? In Windows, an OSError is raised.


emacs - Python Code Completion

After using C# for long time I finally decided to switch to Python. The question I am facing for the moment has to do about auto-complete. I guess I am spoiled by C# and especially from resharper and I was expecting something similar to exist for Python. My editor of choice is emacs and after doing some research I found autocomplete.pl, yasnippet and rope although it is not clear to me if ...


python - How to show sorted list by overall completion in Pootle

Hi I am new to Django and working on Pootle project. I would like to sort by Overall completion by default in the pootle index page. for example, http://pootle.locamotion.org/ lists the languages and sorted by names. you can click sort buttons to see sort by completion. but I would like to show this list sorted by completion whenever load a page.


Python IDE with auto completion

please advise me some good Python IDE, I was using netbeans but it does not have suitable code completion (when I press "." it gives me methods of all classes of python. It would be nice if netbeans would work as for ex. for PHP.. Thank you.


python - completion of part x using n-1 parts

I've build a small module in python which takes a list of strings/buffers in the same size, and returns a xor string in the same size. Then, using that string, together with n-1 strings, I can complete the missing one. It works great, so my question is: do you know an already made python module for that? is there a way (practically/theory) where i can complete 2 missing stri...


python - Pydev Code Completion for everything

In many cases (such as function parameters) Pydev doesn't statically know the type of a variable. Therefore code completion (after . or when using ctrl+space) doesn't work. In most cases, you know what type will be in run-time as you are designing the software. Is there a way to hint Pydev to code completing it correctly?


python - Create a vim script for django code completion, but fail, why?

I tried two ways to do code completion, one is OK, the other fails. OK one does like below: $> cd myDjangoProject/ myDjangoProject $> export PYTHONPATH="." myDjangoProject $> DJANGO_SETTINGS_MODULE=settings vim urls.py Then ^x ^o can work well. But this method leads me repeatly do above when edit a file in project. So an idea comes to me, why not c...


emacs python code completion

This question already has answers here:


python code completion in Emacs

I tried make PythonIDE using emacs like in this article http://www.enigmacurry.com/2009/01/21/autocompleteel-python-code-completion-in-emacs/ But emacs said me "auto-complete-mode not enabled". It is possible use emacs for python coding?


Has anyone found a good set of python plugins for vim -- specifically module completion?

I'm looking for a suite of plugins that can help me finally switch over to vim full-time. Right now I'm using Komodo with some good success, but their vim bindings have enough little errors that I'm tired of it. What I do love in Komodo, though, is the code completion. So, here's what I'm looking for (ordered by importance). Code completion, meaning: the ability to code complete modules/functi...


Python tell when an ftp transfer sits on completion

I have to download some files from an FTP server. Seems prosaic enough. However, the way this server behaves is if the file is very large, the connection will just hang when the download ostensibly completes. How can I handle this gracefully using ftplib in python? Sample python code: from ftplib import FTP ... ftp = FTP(host) ftp.login(login, passwd) files=ftp.nlst() ftp.set_debuglevel(2)...


python tab completion in windows

I'm writing a cross-platform shell like program in python and I'd like to add custom tab-completion actions. On Unix systems I can use the built-in readline module and use code like the following to specify a list of possible completions when I hit the TAB key: import readline readline.parse_and_bind( 'tab: complete' ) readline.set_completer( ... ) How can I do this on Windows? I'd like to...


python - IO completion port key confusion

I'm writing an IO completion port based server (source code here) using the Windows DLL API in Python using the ctypes module. But this is a pretty direct usage of the API and this question is directed at those who have a knowledge of IOCP, not Python. As I understand the documentation for...


Getting proper code completion for Python on Vim?

I've gotten omnicompletion with Pysmell to work before, but I can't seem to do it again. I tried following some steps online, but most, if not all, of them are to vague and assume too much that you know what you are doing to some extent. Can someone post a full, step-by-step tutorial on how to get code completion working properly, for complete Vim newbies (for dummies?)?


python - Trick code completion with PyDev like with PDT?

Is there a way to help PyDev code completion by telling it the type of a variable? With PDT, you can use PHPDoc-like syntax for such purpose: /* @var $my_var MyClass */ $my_var = myFunction(); // PDT is able to figure out that $my_var is a MyClass object. But till now, I cannot figure out how to do the same in python.


mercurial - Tab Completion in Python Command Line Interface - how to catch Tab events

I'm writing a little CLI in Python (as an extension to Mercurial) and would like to support tab-completion. Specifically, I would like catch tabs in the prompt and show a list of matching options (just like bash). Example: Enter section name: ext*TAB* extensions extras The problem is I'm not sure how to catch the Tab events. I'm using the ui.prompt() API of Merc...


Code completion in NetBeans' python plugin does not work properly

I am asking on StackOverflow because surely I am doing something completely silly and I hope S.O. might provide me with a quick answer. I've installed the latest stable Python-plugin for NetBeans. It works great, and I tested code completion with various packages such as sys, os and so on. It works beautifully. However, it does not seem to pick up the code completion for the code in my ow...


python - Weird behaviour with optparse and bash tab completion

I am building a script for users new to Linux, so please understand why I am asking this :) My script runs like this: python script.py -f filename.txt I am using the optparse module for this. However, I noticed the following when doing tab completion. The tab completion works when I do: python script.py <tab completion> # Tab completion wo...


python - which inotify event signals the completion of a large file operation?

for large files or slow connections, copying files may take some time. using pyinotify, i have been watching for the IN_CREATE event code. but this seems to occur at the start of a file transfer. i need to know when a file is completely copied - it aint much use if it's only half there. when a file transfer is finished and completed, what inotify event is fired?






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top