Run a script in the app¶
Open a Python script in the app, run it, and find its results in the workspace beside those of the dialogs: each statement it runs is a step in History, and its objects can be opened, tested and reported like any other. The script stays an ordinary Python file, which runs the same way outside the app.
Before you start¶
- A script. This page uses
consumption.py, which estimates the error-correction equation for consumption of Estimate an equation and test its residuals and works out its speed of adjustment:
```text consumption.py from pymetrix import Dataset, Equation
data = Dataset.example("sample_macro") ecm = Equation("dlog(c) ~ dlog(y) + log(c)[-1] + log(y)[-1]").fit(data) speed = -float(ecm.params["log(c[-1])"]) print(f"speed of adjustment: {speed:.2f}") ```
- The Immediate window, a Jupyter console, for the script's printed output: it comes with
pip install pymetrix[qt](qtconsole). Without it scripts still run, and an error is shown in a message box instead. - An EViews program is not a Python script: Translate an EViews program turns one into a script first.
In the app, or in code¶
- File → Open script…, and choose
consumption.py. It opens in a Code window, with its name in the title. - ▶ Run (F5) runs the whole script through the Immediate window, one statement at a time.
data,ecmandspeedappear in Objects, the printed line in the Immediate window ("speed of adjustment: 0.75"), and each statement in History, as a step of its own. - Edit a line, then Run selection (F9) runs the selected lines, or the line the cursor is on, and moves to the next line. A line that fails is highlighted, the run stops there, and History shows the step in red.
- Open
ecmfrom Objects and carry on with the dialogs: Next ▸ → Test…, say, or Test → Run test… with Object to test: ecm. - View → Script shows the session as one script: the code as run, and the dialogs' steps after it as code. File → New script from session opens it in a Code window; File → Export script… saves it.
- Save (Ctrl+S while the Code window has the focus) writes the script back to
consumption.py.
Check the result¶
- Objects lists
data,ecmandspeed;speedis 0.75, as when the script runs on its own. - History has the script's steps, each with its code; a step that failed is red.
- View → Script starts with
from pymetrix import *and has the script's lines: run in a fresh Python, it gives the same objects.
Pitfalls¶
- Files are read from the app's working folder, not the script's. A script that reads
data.csvby a relative path finds it only if the app was started in that folder. Use full paths, orPath(__file__).parentin a script run withpython. - Typed code runs only in a trusted project. A project with code typed or run in the app asks before running it when someone else opens it (Save, reopen and share a project, and replay its script).
- A script's names replace the workspace's. A script that assigns
datareplaces the datasetdata, and the results made from the old one are marked out of date in Objects. Give a script's objects names of their own. - Run takes the text in the window, saved or not. Save it (Ctrl+S) to keep what ran.
- IPython-only syntax.
%timeitandx?work in the Immediate window and are recorded, but appear in the script as comments.
See also¶
- Reading and writing projects from programs, in the help.
- The user guide's History, Immediate and Code windows: completion, Help on a name, and what History can do with the steps.
- Save, reopen and share a project, and replay its script.