pyExecfile does not work on windows

Create issue
Issue #6 resolved
Rongli H created an issue

i am using windows 7, R version 3.2.2, python 2.7. pyExecfile throws error of not finding the file even though the file exists.

Warning message:
packagePythonInRwas built under R version 3.2.5 
> pyConnect()

Initialize Python Version 2.7.10 (default, Aug 21 2015, 14:42:12) [MSC v.1500 32 bit (Intel)]
> file.exists("C:\\temp\\")
[1] TRUE
> pyExecfile("C:\\temp\\")
Traceback (most recent call last):

  File "<string>", line 1, in <module>

[Errno 2] No such file or directory: 'C:\temp\'
Error in pyExecp(sprintf("execfile('%s')", as.character(filename)[1])) : 
  An error has occured while executing Python code. See traceback above.

The above error happens to windows only. It works fine on Linux.

To find an alternative, I tried the following on both Windows and Linux and it just caused R to terminate:

>myExec = pyFunction("execfile")
 *** caught segfault ***
address 0x8, cause 'memory not mapped'

 1: .Call("py_call_obj", callableObj, args, kwargs, simplify, autoTypecast)
 2: pyCall("execfile", args = xargs, kwargs = xkwargs)
 3: myExec("<pathToPyFile>")

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace

Is this an issue with pyExecfile for windows? What are the alternatives to execute an ,py file in R?

Comments (4)

  1. Rongli H reporter

    I think the main reason is that PythonInR cannot interprete a directory in Windows. I solved pyExcefile problem after moving the to the currect working directory, so pyExcefile("") works. Then I wanted to change cwd by doing pyExec("os.chdir('C:\temp')"), I get error: ...The fileanme, directory name, or volume label syntax is incorrect:...

  2. Florian Schwendinger repo owner


    This is kind of interesting. Generally I would always use,



    The part where it get's kind of interesting is, the following three all work,


    where I would assume pyExec("os.chdir('C:\\Users')") shouldn't work.


    As you reported


    doesn't work since \\ gets evaluated to \ and execfile needs \\ in a path. However




    The same is true for pyExec(execfile('...'))


    will throw an error, but




    And I agree, I should add some check which changes \\ to \\\\ as os.chdir seems to change \ to \\.

    Best, Florian

  3. Log in to comment