1717console_locals = globals ().copy ()
1818
1919import code
20- import readline
2120import rlcompleter
2221import sys
2322
23+ try :
24+ import readline
25+ except ImportError :
26+ pass # readline is not available on all platforms
27+
28+
2429class DynamicCompleter (rlcompleter .Completer ):
2530 """
2631 A custom completer that dynamically updates its namespace to include new
@@ -47,18 +52,19 @@ def complete(self, text, state):
4752 sys .ps1 = ""
4853 sys .ps2 = ""
4954
50- # Set up tab completion.
51- completer = DynamicCompleter (console_locals )
52- readline .set_completer (completer .complete )
55+ if "readline" in globals ():
56+ # Set up tab completion.
57+ completer = DynamicCompleter (console_locals )
58+ readline .set_completer (completer .complete )
5359
54- # TODO(jpwoodbu): Use readline.backend instead of readline.__doc__ once we can depend on having
55- # Python >=3.13.
56- if ' libedit' in readline .__doc__ : # type: ignore
57- readline .parse_and_bind ("bind ^I rl_complete" )
58- elif ' GNU readline' in readline .__doc__ : # type: ignore
59- readline .parse_and_bind ("tab: complete" )
60- else :
61- print (' Could not enable tab completion!' )
60+ # TODO(jpwoodbu): Use readline.backend instead of readline.__doc__ once we can depend on having
61+ # Python >=3.13.
62+ if " libedit" in readline .__doc__ : # type: ignore
63+ readline .parse_and_bind ("bind ^I rl_complete" )
64+ elif " GNU readline" in readline .__doc__ : # type: ignore
65+ readline .parse_and_bind ("tab: complete" )
66+ else :
67+ print (" Could not enable tab completion!" )
6268
6369# We set the banner to an empty string because the repl_template.py file already prints the banner.
6470code .interact (local = console_locals , banner = "" , exitmsg = exitmsg )
0 commit comments