I like the idea of rlcompleter every time you launch python, but so far in various tests have not been able to get it working. So today (to get Boilerplate Python back running) I’m spending some time playing with rlcompleter.
It turns out that with a few tricks, we can have a cross-platform rlcompleter function, that works on Linux/Windows/OSX all in one file (this is new hotness). On windows, you will need to install pyreadline for windows, but we can rock that.
#Code UUID = '9301d536-860d-11de-81c8-0023dfaa9e40'
import sys
try:
import readline
except ImportError:
try:
import pyreadline as readline
# throw open a browser if we fail both readline and pyreadline
except ImportError:
import webbrowser
webbrowser.open("http://ipython.scipy.org/moin/PyReadline/Intro#line-36")
# throw open a browser
#pass
else:
import rlcompleter
if(sys.platform == 'darwin'):
readline.parse_and_bind ("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
Run that script on Windows, Linux, or OSX, and you (should) get tab completion for both basic commands, and files. And if you don’t have readline installed on windows, it will launch a web browser right to pyreadline. Readon, there are some details on how to get it running on every launch in python interactive console. Read on!
So, for tab complete to run when you run the interactive console, you have to setup a launch file to run everytime that python starts. In general, this is done by setting up a PYTHONSTARTUP system variable.
On OS X and Linux:
Create an file ‘.pythonrc’ in your home directory, and put that code into it. This is a file to launch each time that python starts (in the command line environment that is).
Then set an environment variable ‘PYTHONSTARTUP to points to that file. You will probably want to create (or append it to) .profile in your home directory.
PYTHONSTARTUP= ~/.pythonrc
not into ‘~/.MacOSX/environment.plist’
On Windows:
You need to create a similar file (here we called it ‘.inputrc’ and put it in your home directory.
you can even try importing your standard bash/command-line into your environment too:
import os.path
# history file
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
readline.read_history_file(histfile)
except IOError:
pass
#1 by jedijf on 2009.09.02 - 19:56
Quote
Got it in Ubuntu! Ubuntu has no PYTHONSTARTUP environment variable, so all you have to do is:
jimf@tosh:~$ export PYTHONSTARTUP=~/.pythonrc
Now the variable exists and is set – everything else will now work.
#2 by jedijf on 2009.09.02 - 20:00
Quote
jimf@toshiba:~$ cat .pythonrc
try:
import readline
except ImportError:
# Silently ignore missing readline module
pass
else:
import rlcompleter
readline.parse_and_bind(“tab: complete”)
jimf@toshiba:~$
#3 by jedijf on 2009.09.03 - 18:45
Quote
Forget the environment variable nonsense – add to .profile
PYTHONSTARTUP= ~/.pythonrc
jimf@toshiba:~$ grep -i PYTHON .profile
PYTHONSTARTUP= ~/.pythonrc
Doh!
#4 by jedijf on 2009.09.03 - 20:04
Quote
OK, neither of those two options remain persistent through reboots; .profile nor export env – to remain persistent i added PYTHONSTARTUP=~/.pythonrc to /etc/environment.