|
Size: 2512
Comment:
|
Size: 2602
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 13: | Line 13: |
| The path to the Python executable is set using: | Most system configuration is done with the '''`python set`''' command. Stata can list recognized Python environments with `python search`. To add an unrecognized environment, try: |
| Line 16: | Line 18: |
| python set exec "path_string" | python set exec "C:\path\to\python\installation" |
| Line 18: | Line 20: |
Stata can list recognized Python environments with `python search`. |
|
| Line 24: | Line 24: |
| python set userpath "path_string" [...], [prepend] | python set userpath "C:\foo" "C:\bar" "C:\baz", [prepend] |
| Line 27: | Line 27: |
| To make these settings permanent, use the `permanent` option. | To make these settings permanent, add the '''`permanent`''' option. |
| Line 35: | Line 35: |
| === Python REPL === | |
| Line 37: | Line 36: |
| A Python REPL is entered with the `python` command. It prints to the screen a reminder that the `end` command is used to exit the environment. | === REPL === A Python REPL is entered with the `python` command alone. It prints to the screen a reminder that the `end` command is used to exit the environment. |
| Line 52: | Line 54: |
| Within the REPL, the `stata` context submits commands to the parent Stata shell. | Within the REPL, prefix a Stata command with `stata:`. |
| Line 60: | Line 62: |
| === Python Scope === A Python scope can be entered with `python:` (similar to defining function in Stata). This Python environment is exited upon leaving the scope, i.e. the end of the statement. To submit multiple statements, delimit them with semicolons. === Python SFI Module === |
=== SFI === |
| Line 76: | Line 72: |
| This can even be used within the Python REPL that is running within the Stata shell. | This can be used within a REPL as shown above. |
| Line 80: | Line 76: |
| === Stata Do Files === | === Scope === |
| Line 82: | Line 78: |
| Python scopes within a Do file are entered with `python:` and terminated with `end`. | A Python scope can be entered with `python:`. This Python environment lasts only until the end of the statement. To submit multiple statements on a scope, delimit them with semicolons. === Do Files === While an interactive `python:` scope lasts only until the end of the statement, a do file `python:` scope continues until explicitly terminated by the `end` command. |
| Line 93: | Line 97: |
| A common strategy is to define Python functions within a Do file, then create Stata functions to interface. | A common design pattern is to define Python functions within an [[Stata/AdoFiles|ado file]] with the primary Stata function being a thin interface for them. |
Stata Python
Stata supports calling out to an embedded Python interpretter.
Contents
Installation
Most system configuration is done with the python set command.
Stata can list recognized Python environments with python search. To add an unrecognized environment, try:
python set exec "C:\path\to\python\installation"
To prepend or append to the PYTHONPATH, use:
python set userpath "C:\foo" "C:\bar" "C:\baz", [prepend]
To make these settings permanent, add the permanent option.
Usage
REPL
A Python REPL is entered with the python command alone. It prints to the screen a reminder that the end command is used to exit the environment.
Stata local variables are accessed with quotations.
. local int_var = 3
. local str_var = "This is a Stata string"
. python
---------------------------------------- python (type end to exit) -----------
>>> `int_var'
3
>>> "`str_var'".split(" ")
['This', 'is', 'a', 'Stata', 'string']Within the REPL, prefix a Stata command with stata:.
>>> stata: sysuse auto, clear
SFI
A foreign function interface module is also available.
from sfi import Data
pymake = Data.get('make')
# do somethingThis can be used within a REPL as shown above.
Scope
A Python scope can be entered with python:. This Python environment lasts only until the end of the statement.
To submit multiple statements on a scope, delimit them with semicolons.
Do Files
While an interactive python: scope lasts only until the end of the statement, a do file python: scope continues until explicitly terminated by the end command.
sysuse auto, clear python: from sfi import Data pymake = Data.get(’make’) # do something end
A common design pattern is to define Python functions within an ado file with the primary Stata function being a thin interface for them.
program varsum
version 16.0
syntax varname [if] [in]
python: _varsum("`varlist'", "`touse'")
display as txt " sum of ‘varlist’: " as res r(sum)
end
python:
from sfi import Data, Scalar
def _varsum(varname, touse):
x = Data.get(varname, None, touse)
Scalar.setValue("r(sum)", sum(x))
end. sysuse auto, clear (1978 Automobile Data) . varsum price sum of price: 456229 . varsum price if foreign sum of price: 140463
