Announcement

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

  • Is there a way to make Python stop on error when running PyStata ?

    Suppose I have the following (toy) code
    Code:
    import stata_setup
    stata_setup.config("C:/Program Files/Stata17", "mp")
    
    for i in range(1000):
     %stata replace y=2
    and I run the whole thing in python. Suppose Stata throws an error that variable y does not exist. If it were the real Stata, this would stop the program, and I would be happy. But in python, it will be executed 1000 times! Is there any way to stop it on its own?
    Last edited by John Williamss; 25 Jan 2023, 19:01.

  • #2
    What software are you using to edit and run your python script?

    Comment


    • #3
      Originally posted by Daniel Schaefer View Post
      What software are you using to edit and run your python script?
      Spyder but I don't think it matters.

      Comment


      • #4
        It does matter. It is the responsibility of the IDE to stop on an exception. Just one example of many; pycharm will not stop on an exception when you press "run", but it will stop on an exception when you press "debug". This is useful in software engineering because it allows you to test compiled code without symbolic linking before you release the product. Python is compiled to bycode, so this is a feature (not a bug) in python.

        Start by wrapping your example code (minus the loop) in a try except statement and print the type of exception when you hit the except block to the console. This will confirm that an exception is thrown by the Stata "magic" command. Next, check to make sure the IDE is properly configured and that you are running the script in debug mode. If the magic command throws an exception at all, I guarantee the IDE is misconfigured.

        Comment


        • #5
          Originally posted by Daniel Schaefer View Post
          It does matter. It is the responsibility of the IDE to stop on an exception. Just one example of many; pycharm will not stop on an exception when you press "run", but it will stop on an exception when you press "debug". This is useful in software engineering because it allows you to test compiled code without symbolic linking before you release the product. Python is compiled to bycode, so this is a feature (not a bug) in python.

          Start by wrapping your example code (minus the loop) in a try except statement and print the type of exception when you hit the except block to the console. This will confirm that an exception is thrown by the Stata "magic" command. Next, check to make sure the IDE is properly configured and that you are running the script in debug mode. If the magic command throws an exception at all, I guarantee the IDE is misconfigured.
          I wrapped according to your instruction and %stata does not throw any error. On the other hand, stata.run throws an error. This is pretty limiting as %stata is more convenient.

          Comment


          • #6
            Sorry to hear that. It doesn't look like there is any reason why IPython should prevent a command like this from raising an exception, so this was apparently an implementation choice. You might be able to cut this gordian's knot by using the multi-command %%stata magic and writing the for loop in Stata native.

            Comment


            • #7
              Originally posted by Daniel Schaefer View Post
              Sorry to hear that. It doesn't look like there is any reason why IPython should prevent a command like this from raising an exception, so this was apparently an implementation choice. You might be able to cut this gordian's knot by using the multi-command %%stata magic and writing the for loop in Stata native.
              Unfortunately, the only way is by stata.run() . the magic %%stata does not work in a python loop.

              Comment


              • #8
                I am suggesting that, rather than wrapping the line in the python loop, you might be able to write the loop you need in Stata's ado language using the multiline magic command. Of course, there may be cases where that isn't appropriate. It depends on what, exactly, you are trying to do.

                Comment


                • #9
                  It may help to understand this puzzling %stata behavior to know that pystata.run used to also not raise an error (contrary to its documentation) until a bug fix in November of last year.

                  Comment


                  • #10
                    Originally posted by Tim Huegerich View Post
                    It may help to understand this puzzling %stata behavior to know that pystata.run used to also not raise an error (contrary to its documentation) until a bug fix in November of last year.
                    Pretty interesting. I should probably report the bug there.

                    Comment


                    • #11
                      Originally posted by John Williamss View Post

                      Unfortunately, the only way is by stata.run() . the magic %%stata does not work in a python loop.
                      As an aside, the `sfi` package provides another way to run single-line commands, a way which can be much faster than `stata.run` (or `%stata`) in the looping scenario you describe: sfi.SFIToolkit.stata()

                      I'm not sure whether this is widely known. For details (and some caveats), see: https://hugetim.github.io/nbstata/stata.html#run_single

                      Comment

                      Working...
                      X