
Enable tab-completion in the interactive interpreter by default - tshepang
http://hg.python.org/cpython/rev/d5ef330bac50
======
est
To use tab completion in earlier Python versions:

    
    
        import readline, rlcompleter; readline.parse_and_bind("tab: complete")

~~~
niggler
on OSX you need

    
    
        readline.parse_and_bind('bind ^I rl_complete')

~~~
alexpopescu
I use a combination of all these 3 tricks as I have my dotfiles shared between
Mac and Linux.

Basically my pythonstartup.py contains the extra check for os:

    
    
        AP_AUTOCOMPLETE=False
        import sys
        try:
            import readline
        except ImportError:
            print "Module readline unavailable"
        else:
          import rlcompleter
          readline.parse_and_bind("tab: complete")
          if sys.platform == 'darwin':
                readline.parse_and_bind("bind ^I rl_complete")
          AP_AUTOCOMPLETE=True

------
Camillo
One problem with tab completion is that you can no longer paste tab-indented
snippets into the intepreter, or use tab to indent code you're typing. I put
the following in my python startup file to fix the issue:

    
    
        import readline, rlcompleter
        readline.parse_and_bind("tab: complete")
        readline.parse_and_bind('bind ^I rl_complete')
        
        class MyCompleter(rlcompleter.Completer):
        	def complete(self, text, state):
        		if text.lstrip() == '':
        			if state == 0:
        				return text + '\t'
        			else:
        				return None
        		else:
        			return rlcompleter.Completer.complete(self, text, state)
        
        readline.set_completer(MyCompleter().complete)

~~~
alexpopescu
Niiiice!

------
obviouslygreen
Wow, it's about time. It's not that hard to set up tab completion using a
startup script, but that always struck me as sort of a backwards thing to have
to do every time I set up a new server or workstation.

------
ak217
Tangentially related: Shell tab completion for Python programs using argparse:
<https://github.com/kislyuk/argcomplete>

------
Goranek
I always wondered why default interpreter can't be more like bpython.

Good work

~~~
thauck
Some would argue why it can't be more like IPython. :)

But agreed, good work... tab completion is very helpful when learning a
language.

~~~
Goranek
IPython seems to be "too heavy" for me compared to Bpython :)

Bpython is perfect for me.

~~~
gknoy
The thing that soured me on bpython, after loving it for months, was the
realization that I could not scroll back easily to see results of past things
(aside from the most recent one). Ipython (and, even better, the qtconsole for
it) does that much better.

In a console, I like bpython's interface and autocomplete suggestion
interface, but being unable to scroll back and see what I did 3 commands ago
was a deal killer.

------
dmd
This is great, but is there any reason whatsoever not to use ipython?

~~~
aaronharnly
I use virtualenv for all projects, and frequently destroy and recreate
virtualenvs. iPython takes a good while to install; doing it over and over
again in fresh virtualenvs gets to be a chore, so I tend to do it only when I
really need it.

I dunno, is there a better way, maybe installing iPython globally, but
rewriting the shebang line to use /usr/bin/env python?

~~~
tshepang
You can always install it directly, and not in a virtualenv. Am I missing
something?

~~~
Buttons840
If it's installed globally then it wont have access to the libraries installed
only in the virtualenv. I think there's a way to work around this, but I
haven't taken the time to look into it.

~~~
eclipticplane
This should be fixed in the latest iPython versions. iPython is now
virtualenv-aware -- it'll add your venv path at startup even if iPython is
installed in global site-packages.

------
tshepang
The related Issue has been open since 2009:
<http://bugs.python.org/issue5845>.

------
digisign
I've always wondered why so many of these usability niceties have been
available yet neglected soooo long, while the language as a whole has
progressed so much? There are plenty more of these, like interpreter history
and adding pip to the stdlib.

On Windows it's even worse, newbies have to figure out how to get it in their
path and hunt-down important libraries.

~~~
tshepang
The mystery goes away once you start reading python-dev and watching
bugs.python.org... people find it hard to agree on the right way to do things.
It takes a lot of energy for the bigger changes to go in, and many of those
with the energy are busy working on other things, while the Issues rot.
CPython needs more contributors (as does so many volunteer software projects).

~~~
mixmastamyk
BTW, are you implying/do you think I could get some of these in with some
enthusiasm and a bit of work (given they've been implemented for a decade).

~~~
tshepang
You have a better chance of success if you try. Even respected core
developers, including Guido himself, fail to get stuff they want in. So, no,
it's no guarantee.

------
spicavigo
I don't remember the last time I used anything except iPython. I use EC2 a
lot, and the first thing I do after firing an instance is install ipython,
even if I don't intend to use the instance for python related stuff at all.

------
jayflux
Antoine Pitrou has certainly contributed a lot to CPython.

good work

~~~
eliben
He most certainly did! But did you notice the commit says "original patch by
Éric Araujo" ?

------
oellegaard
Will this be on 2.7.x or just 3.x?

~~~
tshepang
3.4, early next year <http://www.python.org/dev/peps/pep-0429>

~~~
circuiter
Is there anything that would prevent this from being backported to 2.x.x ?

~~~
tshepang
None of the releases deemed final accept any new features. It's general
release policy for any FLOSS project, and only neglected on rare occasions.

~~~
cben
There is a recent PEP excluding IDLE from this policy, allowing it to get
usability improvements on older pythons. One could think that enabling
completions is a similar usability fix (though one change isn't worth the
arguing to get exception) but in fact this change required a new var in `sys`
and changed site.py, both unacceptable for a minor release.

That's even for backport to 3.3. As for 2.7, even when when allowed, most
changes aren't backported to it simply because the code has diverged a lot and
core devs are tired of implementing changes twice.

------
jfb
Next up: sqlplus.

~~~
e1ven
I know that this is about as useful as the people pointing to ipython, but
gqlplus really does do wonders.

