Announcement

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

  • call Python script from stata do file

    Dear all,

    This is a technical question. Imagine I have a python script called "test.py". I know that I can type python in the command and type out my python script line by line. But this "test.py" is long, I was wondering how to call this python script from stata's do file. I did the following in a stata do file, but apparently, it does not work.

    cd "my_path"
    python test.py


  • #2
    I will post my code when I'm home from the orthodontist office, but, to run a full python script in your do file, I believe you do
    Code:
    python: filename(arguments)
    I could be wrong here (not thinking straight due to tooth pain), but I believe this is it.

    Comment


    • #3
      Code:
      help python

      Comment


      • #4
        Originally posted by Bjarte Aagnes View Post
        Code:
        help python
        OK. Nice, I forgot the help function.
        Another issue, the following is my do file:

        Code:
        clear all
        cd "my_path"
        set python_exec "my_python_path"
        python script test.py
        Everytime I rerun this do file, I got error "Python has been initialized. You need to restart Stata to set the Python executable"
        Is there solution to this issue? Thanks.

        Comment


        • #5
          Doesn't Stata read your Python installation & executable path from your operating system's environmental variables? If so, then you shouldn't usually need to set the executable path, absence some special requirement regarding Python version.

          Show what you get from the following.
          Code:
          local executables : dir "my_path" files "*.exe"
          foreach executable of local executables {
              display in smcl as result "`executable'"
          }
          You should see python.exe somewhere in that listing, at least if you using Windows. If not, then you're misleading Stata about where Python is installed on your machine.

          Comment


          • #6
            Sorry, that should have been
            Code:
            local executables : dir "my_python_path" files "*.exe"
            You can also check
            Code:
            python search
            in order to see whether the path that you've specified is among those listed.

            Comment


            • #7
              python set exec should be run once before any python code. Rerunning your code, in the same session, will give the error you see. capture noi python set exec may be used as quick fix.

              Comment


              • #8
                Originally posted by Bjarte Aagnes View Post
                python set exec should be run once before any python code. Rerunning your code, in the same session, will give the error you see. capture noi python set exec may be used as quick fix.
                Yes, I see. capture noi does not work. I guess I have to end the current session if I want to rerun the do file.

                Comment

                Working...
                X