Skip to content

Instantly share code, notes, and snippets.

@GabLeRoux
Created January 23, 2014 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GabLeRoux/8584525 to your computer and use it in GitHub Desktop.
Save GabLeRoux/8584525 to your computer and use it in GitHub Desktop.
pythonrc with autocompletion! :)
import atexit
import os
import sys
try:
import readline
except ImportError:
print "Module readline not available."
else:
import rlcompleter
readline.parse_and_bind('tab:complete')
print(".pythonrc :: AutoCompletion Loaded")
# History
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
print(".pythonrc :: history saved to " + historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
atexit.register(save_history)
# anything not deleted (sys and os) will remain in the interpreter session
del atexit, readline, rlcompleter, save_history, historyPath
@GabLeRoux
Copy link
Author

add export PYTHONSTARTUP=~/.pythonrc to your bashrc or zshrc file ;)

@bhavinmoriya
Copy link

Hi,

I have installed python3.8. I did exactly as you said, and as a result all works fine with python2.7 but not with v3.8. How can I settle this.
Thank you very much.

@GabLeRoux
Copy link
Author

Hi @bhavinmoriya

I wrote a blog post concerning this a long time ago, you can follow it here:
https://gableroux.com/python/2016/01/20/python-interpreter-autocomplete/

I confirm it still works fine with python3.8:

python3.8
Python 3.8.0 (default, Nov 21 2019, 15:20:55)
[Clang 11.0.0 (clang-1100.0.33.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
.pythonrc :: AutoCompletion Loaded
.pythonrc :: history file: /Users/gableroux/.pyhistory
>>> print('test')
test
>>> exit()
.pythonrc :: history saved to /Users/gableroux/.pyhistory
cat /Users/gableroux/.pyhistory
print('test')

Good luck and stay safe ✌️

@bhavinmoriya
Copy link

bhavinmoriya commented Mar 30, 2020 via email

@GabLeRoux
Copy link
Author

@bhavinmoriya well there are plenty of results online when searching "ModuleNotFoundError: No module named 'readline'". On which OS are you? If you're on windows, this should do the trick:
https://stackoverflow.com/a/51964654/1092815

@bhavinmoriya
Copy link

bhavinmoriya commented Mar 30, 2020 via email

@GabLeRoux
Copy link
Author

Well, there's probably something wrong with your system, I just tried on a fresh ubuntu:18.04 docker image on https://labs.play-with-docker.com/ (a bit different from an actual clean os, but still)

docker pull ubuntu:18.04
docker run --rm -it ubuntu:18.04 bash

apt-get update && apt-get install -y python3 vim
touch ~/.pythonrc
vim ~/.pythonrc
# paste content of file from blog post and write file

vim ~/.bashrc
# paste 'export PYTHONSTARTUP=~/.pythonrc' and write file
source ~/.bashrc
python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
.pythonrc :: AutoCompletion Loaded
.pythonrc :: history file: /root/.pyhistory
>>> print('example')
example
>>>
.pythonrc :: history saved to /root/.pyhistory
cat /root/.pyhistory
print('example')

@bhavinmoriya
Copy link

bhavinmoriya commented Mar 30, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment