Differences between revisions 1 and 2
Revision 1 as of 2019-12-18 15:44:08
Size: 354
Comment:
Revision 2 as of 2019-12-18 15:56:19
Size: 1262
Comment:
Deletions are marked like this. Additions are marked like this.
Line 19: Line 19:
A Python REPL is entered with the `python` command. 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.

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, the `stata` context submits commands to the parent Stata shell.



----

= Python Scope =

A Python scope can be entered with `python:` (similar to defining function in Stata). Then Python environment is exited upon leaving the scope.



----

= Python SFI Module =

A foreign function interface module is also available.

{{{
from sfi import Data
pymake = Data.get('make')
}}}

This can even be used within the Python REPL that is running within the Stata shell.

Setup

The path to the Python executable is set using:

python set exec "path_string"

Stata can list recognized Python environments with python search.

To make this setting permanent, use the permanent option.


Python REPL

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.

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, the stata context submits commands to the parent Stata shell.


Python Scope

A Python scope can be entered with python: (similar to defining function in Stata). Then Python environment is exited upon leaving the scope.


Python SFI Module

A foreign function interface module is also available.

from sfi import Data
pymake = Data.get('make')

This can even be used within the Python REPL that is running within the Stata shell.


CategoryRicottone

Stata/Python (last edited 2025-10-24 16:20:01 by DominicRicottone)