TiesdeKok / ipystata

Enables the use of Stata together with Python via Jupyter (IPython) notebooks.
192 stars 67 forks source link

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

Open dfdthor opened 5 years ago

dfdthor commented 5 years ago

%%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 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 commented 5 years ago

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 commented 5 years ago

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.

huozi07 commented 4 years ago

the same for me, anyone dealt with it?

TiesdeKok commented 4 years ago

@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?
karajimys commented 4 years ago

The same for me, any ideas? image

karajimys commented 4 years ago

stata installed, batch mode works

TiesdeKok commented 4 years ago

@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")
karajimys commented 4 years ago

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

TiesdeKok commented 4 years ago

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")?

Gehrmanm commented 2 years ago

image help!this question ,how can i deal it

XksA-me commented 1 year ago

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.

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