Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Interactive prompt
-
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.
-
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 <code>reload()</code> 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, <code>reload()</code> 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.
-
Just tried. But..
if I modify the module I need to run or open a different one. Then I need to <code>reload()</code> but the changes are not active yet. I need besides to import again the module.
Suppose amodule.py contains only a line:
<code>a=1</code>
Then import :
<code>from amodule import *</code>
a shows 1
then change the script:
<code>a=2</code>
I need to run the edited script. Ok
But if I do:
<code>reload(amodule)</code>
a still is 1
I need to do again:
<code>from amodule import *</code>
and finally a is now 2
-
Well, that's how the <code>reload()</code> function works, it only reloads the module itself, but not names that you've imported <em>from</em> the module (using <code>from amodule import *</code>).
Since you want to have all the functions and variables in the global namespace, I would recommend using <code>execfile('amodule.py')</code> instead of importing/reloading it (the caveat about having to save the file still applies).
-
omz: You are right, <code>execfile('amodule.py')</code> is the solution right now.
Thank you for your answer.