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.

>library(PythonInR)
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\\testPy.py")
[1] TRUE
> pyExecfile("C:\\temp\\testPy.py")
Traceback (most recent call last):

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

IOError
: 
[Errno 2] No such file or directory: 'C:\temp\testPy.py'
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")
>myExec("<pathToPyFile>")
 *** caught segfault ***
address 0x8, cause 'memory not mapped'

Traceback:
 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
Selection: 

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 testPy.py to the currect working directory, so pyExcefile("testPy.py") 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

    Hello,

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

    pyExecfile("C:/temp/testPy.py")
    

    _

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

    pyExec("os.chdir('C:\\Users')")
    pyExec("os.chdir('C:\\\\Users')")
    pyExec("os.chdir('C:/Users')")
    

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

    _

    As you reported

    pyExecfile("C:\\temp\\testPy.py")
    

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

    pyExecfile("C:\\\\temp\\\\testPy.py")
    pyExecfile("C:/temp/testPy.py")
    

    works.

    _

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

    pyExec(execfile('C:\\temp\\testPy.py'))
    

    will throw an error, but

    pyExec(execfile('C:\\\\temp\\\\testPy.py'))
    pyExec(execfile('C:/temp/testPy.py'))
    

    works.

    _

    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