42

running Windows 10

From what I understand python3 has tab completion in the python console already built in but this isn't working for me.

When I hit tab there is no completion:

enter image description here

Im aware there are modules that can do this, but I'd like to use the native feature if it is available to me on windows.

red888
  • 27,709
  • 55
  • 204
  • 392

5 Answers5

60

The builtin completion relies on the GNU readline library.

You may be able to get completion working by installing the python version (pyreadline) of this package on Windows.

python -m pip install pyreadline
not2qubit
  • 14,531
  • 8
  • 95
  • 135
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 8
    Don't understand why this is not part of the site-package for Windows then, license issue? – kakyo Jun 13 '19 at 06:41
  • 3
    Fixes the issue but I can't insert tabs anymore. Not even for running a simple `for` loop – Rochak Agrawal Mar 04 '20 at 19:02
  • 1
    I came here looking for a solution that works with the cmd module on windows 10. pyreadline works well with the cmd module in powershell (although Rochak is correct about tabs), but it's really clumsy to use in git bash and just buggy in interactive mode (python -i). The package also doesn't seem to have been updated in a long time. – DavidW Nov 27 '20 at 09:23
  • 2
    @DavidW: Answering why the "package also doesn't seem to have been updated in a long time": `pyreadline` was initially developed to support IPython (which used it through the 4.x releases). But as of IPython 5.0, they moved to using `prompt_toolkit` (which is much more full-featured and platform independent), and it's pretty clear that once they made that decision, they stopped updating `pyreadline` (`pyreadline` was last updated in September, 2015, a month after IPython 4.0 released; IPython 5.0 released nine months later). – ShadowRanger Mar 30 '21 at 16:42
23

The original pyreadline is no longer maintained and doesn't work on newer versions of Python (>=3.10). Installing pyreadline3 works:

python -m pip install pyreadline3
Epic Wink
  • 796
  • 13
  • 18
Elad
  • 633
  • 5
  • 13
7

I'd discourage use of pyreadline where possible, as it was written to support IPython, and stopped active development when IPython stopped using readline/pyreadline to support their REPL.

As an alternative, I'd suggest IPython itself; it implements their own tab-completion features (using prompt_toolkit as of 5.0) that work in a terminal agnostic fashion. If you install and use ipython, you'll get tab completion and the host of other features it provides to improve the interactive experience. Using the py.exe manager application bundled with modern Python, install it for Python 3 (in an admin elevated command prompt if Python installed for all users) with:

py -3 -mpip install ipython

then to run it:

py -3 -mIPython

If you don't want the whole of ipython just to get these features, the prompt_toolkit folks do provide a minimalist ptpython REPL that is basically "Python with a REPL provided by prompt_toolkit" without all the other IPython bells and whistles.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • `pyreadline` still seems to work for Python 3.9. Is your objection to it pragmatic or ideological? – Craig McQueen Apr 09 '21 at 03:11
  • 1
    @CraigMcQueen: `pyreadline` is attempting to emulate `readline` and it does the job just barely well enough to make IPython "not the worst". It's been ages since I used it on Windows, but I remember finding it incredibly frustrating when little things didn't work quite the way they did on a native `readline` system (basically always for the worse). The switch to `prompt_toolkit` was a major improvement. – ShadowRanger Apr 09 '21 at 10:07
1

Installing following packages should fix this on both python CLI and pyspark shell.

pip install pyreadline
pip install ipython
not2qubit
  • 14,531
  • 8
  • 95
  • 135
  • I used `pyreadline3` instead of `pyreadline`, as the latter is deprecated. I have installed python for WIndows 10 64 bit, run it from powershell, and this fixed to auto-complete. – PfunnyGuy May 11 '23 at 20:06
-2

For Windows consider to use free Visual Studio Community as Python IDE - it has all autocomplete features out of the box for Python shell (Python Interactive Window) you will likely need: for modules, methods, etc.

In below example I've imported my DiveIntoPython.py, once I did it, the name of the module is now into autocomplete suggestions.

Python Interactive Window


If you want to add your own modules paths automatically you should utilize built-in site module as described by odjo: https://stackoverflow.com/a/59411635/2170898 -> just create sitecustomize.py file inside site.USER_SITE dir with this content (don't forget to change actual path):

import site
site.addsitedir(r'C:\My_Projects')
Soup Endless
  • 439
  • 3
  • 10