Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Indentation issue when using python within dofile on OSX

    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:

    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
    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:

    Code:
    def calcsum2(varname, touse): x = Data.get(varname, None, touse) ;Scalar.setValue("r(sum)", sum(x))
    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.

  • #2
    Originally posted by Thibaut Lamadon View Post
    This might be an osx specific issue.
    Or maybe just something awry with your particular setup's not passing multiple spaces through from a (plain text) file.

    What do you see if you run the following from a do-file?
    Code:
    version 17.0
    
    clear *
    
    display in smcl as result strlen("    x = Data.get(varname, None, touse)") - ///
        strlen("x = Data.get(varname, None, touse)")
    
    python:
    
    """def calcsum2(varname, touse):
        x = Data.get(varname, None, touse)
        Scalar.setValue("r(sum)", sum(x))"""
    
    print()
    print("""def calcsum2(varname, touse):
        x = Data.get(varname, None, touse)
        Scalar.setValue("r(sum)", sum(x))""")
    
    end
    
    exit
    Is Stata seeing the spaces (a difference of four)? Do you see the spaces in Stata's Results window for the Python section?

    Comment


    • #3
      Well, it doesn't seem to understand the triple quote at all... it says "SyntaxError: unterminated triple-quoted string literal " at the first triple quote.

      I have gone a different route, moved the python code to a separate python file and simply import that file. This allows to not have to use python code with indentation, but forces me to have 2 files. It's not horrible, just a bit inconvenient and it seems it should work.

      I used both vscode and the do file editor of stata, they both end up giving the same,

      thanks, t.


      Comment


      • #4
        Thibaut Lamadon I use Stata 17/MP on macOS 12.6 on an Intel-based Mac, with python version 3.10.5, and your code runs without any problems.
        Last edited by Hemanshu Kumar; 23 Oct 2022, 22:39.

        Comment


        • #5
          Hi

          I'm having the same issue as Thibaut Lamadon and I need to avoid the option of moving the python piece to a separate file. Does anyone know how to solve this issue that Stata does not seem to handle well the indentation of the Python code within Stata (unless I do everything in one line).

          Here is an example of the error I get when I run this Python code within Stata:

          python
          for x in ["test1","test2"]:
          print(x)
          end
          IndendationError: expected an indentend block after 'for' statement on line 1
          IndentationError: unexpected indent

          Whereas if I run the following, I get no error:

          python
          for x in ["test1","test2"]: print(x)
          end

          Any suggestions for solving this directly in Stata? Thanks a lot!

          Comment


          • #6
            Ok, so the easiest solution we found is to do everything in one line, but with intermediate breaks so that it effectively can be written in several lines (we found this to be more efficient than carrying around several codes):

            python
            for x in ["test1","test2"]: \
            print(x); \
            print("intermediate test")
            end

            PS: By the way, there was a typo in my previous post. The first batch of python code should have said:

            python
            for x in ["test1","test2"]:
            print(x)
            end
            IndendationError: expected an indentend block after 'for' statement on line 1
            IndentationError: unexpected indent

            Comment

            Working...
            X