Forum Archive

Interactive prompt

jose3f23

I need to use the interactive prompt to interpret interactively code that depends on functions and clases in a certain module. In "desktop" interpreter I use :

python -i amodule.py

Other way in standard python is to start the interpreter and import the module and then the python interpreter continues accepting input.

In Pythonista I do not know how easily do the same as:

1.- I do not know how to stop and restart the interpreter (interactive prompt)
2.- reload() function seems broken. Never find the required module (of course previously imported)
3.- So I have only a opportunity to import a module and to reload I need to exit Pythonista. Even sys.exit() seems have no effect.

Thanks in advance.

omz

I'm working on a feature that should make this sort of workflow easier, but with some caveats, you can already do the same.

Most likely, the reason reload() doesn't work for you is that Pythonista only saves script files in the editor when you either run them or close the file (open a different one). So if you run your module from the editor, reload() should work properly in the interactive prompt afterwards. Alternatively (if running the module has side effects you don't want), you could switch to a different file in the editor.

jose3f23

Just tried. But..

if I modify the module I need to run or open a different one. Then I need to reload() but the changes are not active yet. I need besides to import again the module.

Suppose amodule.py contains only a line:

a=1

Then import :

from amodule import *

a shows 1

then change the script:

a=2

I need to run the edited script. Ok

But if I do:

reload(amodule)

a still is 1

I need to do again:

from amodule import *

and finally a is now 2

omz

Well, that's how the reload() function works, it only reloads the module itself, but not names that you've imported from the module (using from amodule import *).

Since you want to have all the functions and variables in the global namespace, I would recommend using execfile('amodule.py') instead of importing/reloading it (the caveat about having to save the file still applies).

jose3f23

omz: You are right, execfile('amodule.py') is the solution right now.

Thank you for your answer.