Created
January 23, 2014 18:52
-
-
Save GabLeRoux/8584525 to your computer and use it in GitHub Desktop.
pythonrc with autocompletion! :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Hi Gabriel,
Thanks a lot for the prompt reply. The unfortunate thing is, on my system
the following message shows up,
Python 3.8.2 (default, Mar 23 2020, 18:52:10)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "/home/bhavin/.pythonrc", line 6, in <module>
import readline
ModuleNotFoundError: No module named 'readline'
I have no idea how to cope up with that. I did google and all with no
success. Sorry for keep bugging you, I will be glad if you could help
me out.
I appreciate your time and effort.
regards,
Bhavin Moriya <https://sites.google.com/site/bhavinmoriya/>
Assistant Professor
The Federal University of Viçosa - Brazil.
Departamento de Matemática <http://www.dma.ufv.br/>
Av. P.H. Rolfs, s/n
36570-000 – Viçosa.
Minas Gerais
Brasil.
My Blog
https://livelifemaverick.blogspot.com/
"Only the gentle are ever really strong."
- James Dean
…On Sun, Mar 29, 2020 at 8:24 PM Gabriel Le Breton ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hi @bhavinmoriya <https://github.com/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 ✌️
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/8584525#gistcomment-3232314>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADX5EVYV7HQ4A6GXQGJQ3SLRJ7KCDANCNFSM4LVMD3TA>
.
@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
I use Ubuntu 18.4. I did install pyreadline but no success.
Bhavin Moriya <https://sites.google.com/site/bhavinmoriya/>
Assistant Professor
The Federal University of Viçosa - Brazil.
Departamento de Matemática <http://www.dma.ufv.br/>
Av. P.H. Rolfs, s/n
36570-000 – Viçosa.
Minas Gerais
Brasil.
My Blog
https://livelifemaverick.blogspot.com/
"Only the gentle are ever really strong."
- James Dean
…On Mon, Mar 30, 2020 at 3:51 PM Gabriel Le Breton ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@bhavinmoriya <https://github.com/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
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/8584525#gistcomment-3233548>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADX5EV7YIG6N6GEOX7H32BLRKDSZRANCNFSM4LVMD3TA>
.
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')
Hi Gabriel,
It is working with python3 in my system. Only with the python3.8 it is not
working.
Will try to fix it. Thanks a lot for all your help.
Bhavin Moriya <https://sites.google.com/site/bhavinmoriya/>
Assistant Professor
The Federal University of Viçosa - Brazil.
Departamento de Matemática <http://www.dma.ufv.br/>
Av. P.H. Rolfs, s/n
36570-000 – Viçosa.
Minas Gerais
Brasil.
My Blog
https://livelifemaverick.blogspot.com/
"Only the gentle are ever really strong."
- James Dean
…On Mon, Mar 30, 2020 at 4:01 PM Gabriel Le Breton ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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 filesource ~/.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')
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/8584525#gistcomment-3233591>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADX5EV5ZDGY2VX637NCD5O3RKDUBFANCNFSM4LVMD3TA>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Good luck and stay safe ✌️