TiesdeKok / ipystata

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

FileNotFoundError #8

Closed mbaker21231 closed 8 years ago

mbaker21231 commented 8 years ago

First, I have been using ipystata and think it is great! I use it all the time on my work computer (Windows 7, Python 3.5), and have gotten it up and running on an Amazon Web Services Server as well.

I have, however, not been able to get it working on my home computer (Windows 10, Python 3.5).

Here is my code and the result:

import ipystata
%%stata
sysuse auto, clear

Executing the last code block above results in:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-4-2e71edc6a31b> in <module>()
----> 1 get_ipython().run_cell_magic('stata', '', 'sysuse auto, clear')

C:\Users\Matthew Baker\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2118             magic_arg_s = self.var_expand(line, stack_depth)
   2119             with self.builtin_trap:
-> 2120                 result = fn(magic_arg_s, cell)
   2121             return result
   2122 

<decorator-gen-125> in stata(self, line, cell, local_ns)

C:\Users\Matthew Baker\Anaconda3\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

C:\Users\Matthew Baker\Anaconda3\lib\site-packages\ipystata\ipystata_magic.py in stata(self, line, cell, local_ns)
    333 
    334         time.sleep(1)
--> 335         out = iPyStata.process_log(self.log_dict[session_id])
    336 
    337         if args.output:

C:\Users\Matthew Baker\Anaconda3\lib\site-packages\ipystata\ipystata_magic.py in process_log(self, log)
     62 
     63     def process_log(self, log):
---> 64         with open(log, 'r+') as log_file:
     65             code = log_file.read()
     66             code = re.sub('^-*\\n', "", code)

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Matthew Baker\\.ipython\\stata\\log_main.txt'

As one can see, for some reason ipystata can't find a file called "log_main.txt". I have tried re-registering Stata, reinstalling ipystata, etc. but can't seem to get it going.

Any help or suggestions would be appreciated!

Matt Baker

TiesdeKok commented 8 years ago

Hi, Matt. Thanks for letting me know, awesome to hear that you find good use for it!

I actually encountered this problem myself on my own personal computer (also Win 10 + Python 3.5), but after I restarted my computer the problem magically disappeared. Unfortunately I was unable to reproduce the bug as it only occurred once, hence the underlying cause is still in my code.

Can you try restarting your computer, I know it is a weird suggestion but that seemed to solve it for me.

My suspicion is that, for some reason, Stata is unable to create the log file which results in it being missing. If the restart did not solve it, can you try running it with the -os argument, or alternatively you can run this in the cell below to reveal the Stata window:

%%stata
reveal all

Does this show any indication that the .log file was not created by Stata?

mbaker21231 commented 8 years ago

TIes --

Your suggestion about looking at things using the -os argument quickly revealed what was wrong! When ipystata is trying to create a log file on my home computer, the following error pops up:

. log using C:\Users\Matthew Baker\.ipython\stata\log_main.txt , text replace
invalid 'Baker' 
r(198);

Evidently, the fact that my directory name has a space in it is creating the problem. Unless you have a really, really easy fix, I'll just uninstall everything in the directory, rename it, and reinstall whatever was there.

Best,

Matt

TiesdeKok commented 8 years ago

Hi Matt,

Of course, that explains. Luckily that is a very easy fix, if you update IPyStata to the newest version (that I just uploaded) it should be fixed.

Let me know if it still doesn't work or if you run into any other issues!

mbaker21231 commented 8 years ago

Ties --

Thanks a million - I actually changed a line in the ipython_magic.py file in the package and was able to get it working (after realizing that renaming directories isn't such a great idea), but it's definitely better to stick with the formal package!

Matt

mbaker21231 commented 8 years ago

The update did the trick...thanks again!