Reputation: 2137
Lets say test.py
has the contents
i_am_a_variable = 1
i_am_also_a_var = 2
while True:
input('> ')
Then, while in the same directory as test.py
, I run python3
. Now then, in the REPL, I execute from test import *
. Since the Python 3 REPL has auto-completion, variables are actually also auto-completed in this infinite input
loop. That is, when I run
$ python3
>>> from test import *
> i_am_a|
(|
represents my cursor) If I press tab, as if my cursor is at the end of the line i_am_a
, it will auto-complete to i_am_a_variable
.
Now, if I run python3 -i test.py
, this behavior does not occur. That is, the REPL will not auto-complete. So what exactly occurs when I run python3 -i test.py
. Does test.py
just run and then globals
gets copied over to the REPL?
This was tested on macOS, In case auto-completion is not available on other python distributions. Thanks.
Upvotes: 0
Views: 587
Reputation: 281381
Python initializes autocompletion when interactive mode is entered. (If you want the details, it calls sys.__interactivehook__
, and one of the things the default __interactivehook__
does is import rlcompleter
and configure TAB to trigger completion.)
When you launch Python in interactive mode and then run from test import *
interactively, Python turns on autocompletion before it hits the input
loop. Autocompletion isn't really intended to affect input
, but it does anyway.
When you run test.py
with python3 -i
, Python doesn't enter interactive mode. It plans to enter interactive mode when test.py
finishes, and it plans to run sys.__interactivehook__
and turn on autocompletion when that happens, but the input
loop happens before it reaches that point.
Upvotes: 2
Reputation: 1108
There is a infinite loop which is executed in your code which tries to read the standard input so it is not autocompleting the variable as the script control is in your code to read the input, not in REPL.
Lets say you test.py
:
i_am_a_variable = 1
i_am_also_a_var = 2
without the loop, REPL executes the code(python -i test.py
) and autocomplete works if not it won't as the REPL executes the input
statement of the loop. The above one which you have told that autocomplete works by import in REPL wouldn't have worked, if am correct.
The final solution reproduced
$ cat test.py
i_am_a_variable = 1
i_am_also_a_var = 2
def Infinite():
while True:
input('> ')
$ python3 -i test.py
>>> Infinite()
> i_am_a
i_am_a_variable i_am_also_a_var
> i_am_a
i_am_a_variable i_am_also_a_var
> i_am_a_variable
> i_am_a
i_am_a_variable i_am_also_a_var
> i_am_a
i_am_a_variable i_am_also_a_var
> i_am_also_a_var
>
Upvotes: 0