I am trying to use stata 17 with python on an M1 mac. I have set up arm64 python using miniforge and I can run python code interactively inside the stata console. I can even use the sfi module to interact with data. This works great.
However, when I try to embed python code inside a do or an ado file, I get an indentation error as if stata didn't pass the text formatting to python. I tried using tabs, spaces (2 or 4), and I even changed the end of lines between LF and CLRF using vs code. They all give me an indentation error. Only when I don't use any indentation in python code things actually work.
Here is the simple do-file I am trying out:
The error when sourcing happens within python at the line indented for python: x =...
it says "IndentationError: expected an indented block after function definition on line 1"
Note that changing the python function to a one liner actually works:
So my question is how do I use indentation in python code within do files? This might be an osx specific issue.
Thank you, t.
However, when I try to embed python code inside a do or an ado file, I get an indentation error as if stata didn't pass the text formatting to python. I tried using tabs, spaces (2 or 4), and I even changed the end of lines between LF and CLRF using vs code. They all give me an indentation error. Only when I don't use any indentation in python code things actually work.
Here is the simple do-file I am trying out:
Code:
capture program drop varsum program varsum version 17 syntax varname [if] [in] marksample touse python: from __main__ import calcsum2 ; calcsum2("`varlist'", "`touse'") display as txt " sum of `varlist': " as res r(sum) end python: from sfi import Data, Scalar def calcsum2(varname, touse): x = Data.get(varname, None, touse) Scalar.setValue("r(sum)", sum(x)) end sysuse auto, clear varsum price
it says "IndentationError: expected an indented block after function definition on line 1"
Note that changing the python function to a one liner actually works:
Code:
def calcsum2(varname, touse): x = Data.get(varname, None, touse) ;Scalar.setValue("r(sum)", sum(x))
Thank you, t.
Comment