mattiasnordin / StataEditor

Stata Editor for Sublime Text 3
48 stars 18 forks source link

Stata 15 Automation on Windows 10 #23

Open emosong opened 7 years ago

emosong commented 7 years ago

Hi @mattiasnordin,

First of all, major kudos and love for the StataEditor package. Thank you!

Some context: I've successfully used StataEditor and Stata 14 on Windows 10 without a problem, but I've encountered an issue when updating Stata to version 15. I've made the appropriate changes to the package's user settings and the steps for automation.

When running the do-file, instead of executing the code, Stata 15 opens up two duplicate windows, where the first window that loads indicates on the bottom left: "C:\WINDOWS\system32". After some time loading, a second Stata window opens with "C:\Program Files\Sublime Text 3". When attempting to run the do-file again, it returns an error on the "Sublime Text 3" Stata window. Once closing the "Sublime Text 3" Stata window and running the do-file again, the code successfully executes on the "system32" Stata window with the correct working directory. At this step, Stata automatically reopens another extraneous "Sublime Text 3" window, which I close out of. After this step, I can finally re-run the do-file continuously without issue on the "system32" Stata window.

Here's what the console says:

Traceback (most recent call last):
  File "StataEditorPlugin in C:\Users\esong\AppData\Roaming\Sublime Text 3\Installed Packages\StataEditor.sublime-package", line 63, in StataAutomate
AttributeError: 'module' object has no attribute 'stata'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 812, in run_
    return self.run(edit, **args)
  File "StataEditorPlugin in C:\Users\esong\AppData\Roaming\Sublime Text 3\Installed Packages\StataEditor.sublime-package", line 154, in run
  File "StataEditorPlugin in C:\Users\esong\AppData\Roaming\Sublime Text 3\Installed Packages\StataEditor.sublime-package", line 68, in StataAutomate
  File "<COMObject stata.StataOLEApp>", line 2, in DoCommand
pywintypes.com_error: (-2147417851, 'The server threw an exception.', None, None)

Is there a way to clean this up? Would love to hear back! Thanks again!

mattiasnordin commented 7 years ago

Hi!

Unfortunately, I don't have access to Stata 15 at the moment, so I'm unable to check what is going on. Maybe someone else using Stata 15 has some input? Hopefully I'll have access soon and can take a look at the problem.

mattiasnordin commented 7 years ago

Actually, looking at the error message again, it is possible that something is wrong with extracting the path. Assuming you haven't customized the path settings, StataEditor tries to use the current path to set directory in Stata. You could try changing the settings for "default_path" to some path which you know exists and see if you still ge an error. It's a bit of a long shot, but might be worth trying.

shuai-zhou commented 7 years ago

@emosong I encountered exactly the same problems with you. Do you fix the problem right now?

emosong commented 7 years ago

@mattiasnordin thank you for the suggestion and the feedback. I did change the settings for the "default_path", but no luck there. I reverted back to version 14 at the moment for work because that operation in Sublime still runs smoothly. Hopefully there are more Stata 15 users over time and we can get this perhaps simple(?) issue sorted out.

@Iorent sorry for the late response. No bueno

mattiasnordin commented 7 years ago

@emosong Ok, too bad! Unfortunately, there's not much I can do without access to Stata 15. As I said in issue #24 , if anyone has found a way to make this work with Stata 15, please let me know.

mattiasnordin commented 6 years ago

I have now tried to fix this issue with version 0.9.0. Please let me know if it works for you.

cpmasesa commented 6 years ago

The fix works for me.

Many thanks @mattiasnordin

mattiasnordin commented 6 years ago

Great, thanks for letting me know!

emosong commented 6 years ago

@mattiasnordin the fix works perfect, thanks. Appreciate you looping back and resolving this issue!

mattiasnordin commented 6 years ago

Great!

mattiasnordin commented 6 years ago

Ok, so unfortunately the fix had other bad side effects (see issue #28). I have therefore decided to "unfix the fix". In version 0.10.0, this issue will be in effect again. Unfortunately, I don't really have a good way to solve it. The solution I used when I originally developed the plugin (for Stata 14 and older) was a bit hackish, and for some reason it no longer works in Stata 15. If anyone has some knowledge of COM, and especially with the win32api package in Python, feel free to contact me! :-)

chris-vecchio commented 6 years ago

Adding a Stata version check to both win32api.WinExec calls fixed the issues I was having calling Stata 15.

def StataAutomate(stata_command):
    """ Launch Stata (if needed) and send commands """
    try:
        sublime.stata.DoCommandAsync(stata_command)

    except:
        if settings.get("stata_version") <=14:
            win32api.WinExec(settings.get("stata_path"))
        sublime.stata = win32com.client.Dispatch("stata.StataOLEApp")
        sublime.stata.DoCommand("cd " + getDirectory())
        sublime.stata.DoCommandAsync(stata_command)
        if settings.get("file_completions") != False:
            sublime.file_list = []
            for file_ext in settings.get("file_completions").split(","):
                find_files("." + file_ext.strip())

Edit: I spoke to soon. The graphs stopped working. I'll keep trying to find a solution.

mattiasnordin commented 6 years ago

Yup, that's was exactly what I was doing in v0.9.0. Using the win32api.WinExec() command was kind of an ugly hack to begin with when I developed the plugin, which I used to make the graphs work, although I don't really know why it makes a difference. Therefore, it's hard to troubleshoot for Stata 15, cause I don't know why it works in the first place :-) Any feedback is appreciated!

chris-vecchio commented 6 years ago

Okay, after trying 50 or so different (often crazy) ideas..I think I've found a possible solution. I discovered this solution after realizing that when I was finally able to see graphs, I wasn't using any custom functions I wrote to try to fix the problem.

My small change was to add a time.sleep(0.1) between the win32api.WinExec call and the win32com.client.Dispatch call in StataAutomate() and StataRestore(). I'm not sure if the required sleep time will vary across machines. Can anyone else confirm that graphs stay open with this change?

def StataAutomate(stata_command):
    """ Launch Stata (if needed) and send commands """
    try:
        sublime.stata.DoCommandAsync(stata_command)

    except:
        win32api.WinExec(settings.get("stata_path"))
        time.sleep(0.1)
        sublime.stata = win32com.client.Dispatch("stata.StataOLEApp")
        sublime.stata.DoCommand("cd " + getDirectory())
        sublime.stata.DoCommandAsync(stata_command)
        if settings.get("file_completions") != False:
            sublime.file_list = []
            for file_ext in settings.get("file_completions").split(","):
                find_files("." + file_ext.strip())
mattiasnordin commented 6 years ago

Awesome, thanks Chris! I changed the time setting to 0.5 seconds by default, since 0.1 did not always work for me. Although I made it a setting, so people can change to what works for them. I just released version 0.10.1 with this change. Very happy to receive feedback on whether this solution works!

nimafazeli commented 6 years ago

Hi @mattiasnordin , Thanks for the great work.

I used the plugin without any issue with stata 14 before (Fall of 2017) but after a recent reinstallation of my os and stata I cannot get it to work anymore. Basically nothing happens after hitting the shortcut. The console shows absolutely nothing either. I tried the plugin with stata 14 and 15 with the same results. I am on a windows 10 machine.

My guess is that there might be an issue with automation. I have tried all those ways of registering stata for automation but I could not be sure whether it is done properly. Is there a way to make sure about it?

mattiasnordin commented 6 years ago

@nimafazeli : Try open the console, when you run a do-file, what message do you get?

shuai-zhou commented 6 years ago

@nimafazeli than happened to me before. the problem for me is that I use some symbols and other languages that STATA cannot recognize. Try to avoid that or reinstall the plugin again and do the setting as instructed.