staticvoidlol
staticvoidlol

Reputation: 43

Python tab-completion not working in interactive console (Ubuntu 20.04)

In my ~/.bashrc I have the following line to set the PYTHONSTARTUP environment variable:

export PYTHONSTARTUP=~/.pythonrc.py

Contents of ~/.pythonrc.py:

try:
    import readline
except ImportError:
    print("Module readline not available.")
else:
    print("TAB-COMPLETE LOADED.")
    import rlcompleter
    readline.parse_and_bind("tab: complete")

When running just

python3

in the terminal the message "TAB-COMPLETE LOADED" is printed and tab-complete works as expected on variables, however when running the following file via

python3 example.py

it doesn't work. The expected message is not printed and it just inserts a tab when pressing tab. It behaves the same regardless of whether the variable was declared in the console or the file.

example.py:

test_variable = 123
import code; code.interact(local=dict(globals(), **locals()))

I can't figure out why my pythonrc.py would be loaded for the former but not the latter. Some help would be greatly appreciated.

Upvotes: 4

Views: 385

Answers (1)

minou
minou

Reputation: 16553

A workaround rather than an answer, but likely good enough for many.

Change example.py to:

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

test_variable = 123
import code; code.interact(local=dict(globals(), **locals()))

Upvotes: 1

Related Questions