ipystata icon indicating copy to clipboard operation
ipystata copied to clipboard

Installing IPyStata for Stata 14 64-bit on Windows 10 & Anaconda3 (64-bit)

Open dfdthor opened this issue 5 years ago • 11 comments

%%stata
display "Hello, I am printed in Stata."

produces


com_error Traceback (most recent call last) C:\Users\Public\Anaconda3\lib\site-packages\win32com\client\dynamic.py in _GetGoodDispatch(IDispatch, clsctx) 88 try: ---> 89 IDispatch = pythoncom.connect(IDispatch) 90 except pythoncom.ole_error:

com_error: (-2147221005, 'Invalid class string', None, None)

During handling of the above exception, another exception occurred:

com_error Traceback (most recent call last) in ----> 1 get_ipython().run_cell_magic('stata', ' ', 'display "Hello, I am printed in Stata." \n')

C:\Users\Public\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell) 2356 with self.builtin_trap: 2357 args = (magic_arg_s, cell) -> 2358 result = fn(*args, **kwargs) 2359 return result 2360

<C:\Users\Public\Anaconda3\lib\site-packages\decorator.py:decorator-gen-127> in stata(self, line, cell, local_ns)

C:\Users\Public\Anaconda3\lib\site-packages\IPython\core\magic.py in (f, *a, **k) 185 # but it's overkill for just that one bit of state. 186 def magic_deco(arg): --> 187 call = lambda f, *a, **k: f(*a, **k) 188 189 if callable(arg):

C:\Users\Public\Anaconda3\lib\site-packages\ipystata\ipystata_magic.py in stata(self, line, cell, local_ns) 318 time.sleep(0.5) 319 self.log_dict[session_id] = os.path.join(self.lib_dir, 'log%s.txt' % session_id) --> 320 self.session_dict[session_id] = win32com.client.Dispatch("stata.StataOLEApp") 321 self.do_dict[session_id] = self.session_dict[session_id].DoCommandAsync 322 self.session_dict[session_id].UtilShowStata(1)

C:\Users\Public\Anaconda3\lib\site-packages\win32com\client_init_.py in Dispatch(dispatch, userName, resultCLSID, typeinfo, UnicodeToString, clsctx) 93 """ 94 assert UnicodeToString is None, "this is deprecated and will go away" ---> 95 dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx) 96 return __WrapDispatch(dispatch, userName, resultCLSID, typeinfo, clsctx=clsctx) 97

C:\Users\Public\Anaconda3\lib\site-packages\win32com\client\dynamic.py in _GetGoodDispatchAndUserName(IDispatch, userName, clsctx) 112 else: 113 userName = str(userName) --> 114 return (_GetGoodDispatch(IDispatch, clsctx), userName) 115 116 def _GetDescInvokeType(entry, invoke_type):

C:\Users\Public\Anaconda3\lib\site-packages\win32com\client\dynamic.py in _GetGoodDispatch(IDispatch, clsctx) 89 IDispatch = pythoncom.connect(IDispatch) 90 except pythoncom.ole_error: ---> 91 IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) 92 else: 93 # may already be a wrapped class.

com_error: (-2147221005, 'Invalid class string', None, None)

dfdthor avatar Oct 04 '19 16:10 dfdthor

import ipystata from ipystata.config import config_stata config_stata('C:/Program Files (x86)/Stata14')

executes without error and the above error occurs after restarting the kernel

dfdthor avatar Oct 04 '19 17:10 dfdthor

C:\Program Files (x86)\Stata14>ls ado docs STATA.LIC vcomp100.dll auto.dta isstata.140 StataMP-64.exe DLXAPI32.DLL Microsoft.VC90.OPENMP StataMP-64_old.exe DLXAPI64.DLL stata.html utilities

C:\Program Files (x86)\Stata14>StataMP-64.exe /Register

C:\Program Files (x86)\Stata14>

ran without error as the the import ipystata bit and restarting the kernel.

get_ipython().run_cell_magic('stata', '-o car_df', 'sysuse auto.dta')

still gives

UsageError: Cell magic %%stata not found.

dfdthor avatar Oct 04 '19 17:10 dfdthor

the same for me, anyone dealt with it?

huozi07 avatar Aug 04 '20 16:08 huozi07

@huozi07 let's see if we can figure out what is wrong. Some questions:

  1. Are you running this in a jupyter notebook / lab session?
  2. What is the exact code that you are executing in the notebook? A screenshot would be helpful here.
  3. Do you have Stata installed on your computer or are just running it directly via the exe?
  4. Does the batch mode work?

TiesdeKok avatar Aug 04 '20 23:08 TiesdeKok

The same for me, any ideas? image

karajimys avatar Aug 08 '20 18:08 karajimys

stata installed, batch mode works

karajimys avatar Aug 08 '20 18:08 karajimys

@karajimys this is not necessarily a problem with ipystata, it is more likely a problem with win32 not being able to run/find Stata.

I assume you get the same error if you run the below in a Jupyter Notebook?

import win32com.client as win32
win32.Dispatch("stata.StataOLEApp")

TiesdeKok avatar Aug 08 '20 21:08 TiesdeKok

@TiesdeKok thank you for the answer. Yes exactly the same error. Any ideas on how could this be solved?

karajimys avatar Aug 09 '20 12:08 karajimys

Ok, that implies that the register step wasn't successful. Did you try to follow the registration steps (https://www.stata.com/automation/#createmsapp) but with an elevated command prompt (i.e. right click on CMD and click "run as administrator")?

TiesdeKok avatar Aug 10 '20 17:08 TiesdeKok

image help!this question ,how can i deal it

Gehrmanm avatar Feb 13 '22 03:02 Gehrmanm

This method solves my problem. It should be a user permission problem, and /Register operation should be performed with administrator permission. I hope it will be helpful to the people behind.

  • Method 1: Create a new cmd/powershell with administrator privileges, and then execute: "C:\Program Files (x86)\Stata15\StataSE-64.exe" /Register .

  • Method 2: Create a StataSE-64.exe shortcut, right-click-properties-target set "C:\Program Files (x86)\Stata15\StataSE-64.exe" /Register, and then right-click to run with administrator privileges.

thanks @TiesdeKok https://www.stata.com/automation/#createmsapp image

XksA-me avatar Jun 13 '23 09:06 XksA-me