Search code examples
pythonrrpy2

Trying to run rpy2 results in error 0x7e, and 'sh' command not found


I've installed R then rpy2 using 'pip install rpy2' in cmd. After running a random simple rpy2 code example found online I get this error message(this error message is actually made by 'python -m rpy2.situation' in cmd because the error message in vscode is a smaller version of this with less information):

C:\Users\richardg>python -m rpy2.situation
rpy2 version:
3.5.13
Python version:
3.11.3 (tags/v3.11.3:f3909b8, Apr  4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)]
Looking for R's HOME:
    Environment variable R_HOME: C:\Program Files\R\R-4.3.1
    InstallPath in the registry: C:\Program Files\R\R-4.3.1
    Environment variable R_USER: C:\Program Files\R\r_user_directory_path
    Environment variable R_LIBS_USER: C:\Program Files\R\r_libs_user
R version:
    In the PATH: R version 4.3.1 (2023-06-16 ucrt) -- "Beagle Scouts"
    Loading R library from rpy2: cannot load library 'C:\Program Files\R\R-4.3.1\bin\x64\R.dll': error 0x7e
Additional directories to load R packages from:
None
C extension compilation:
'sh' is not recognized as an internal or external command,
operable program or batch file.
    Warning: Unable to get R compilation flags.
Directory for the R shared library:
'sh' is not recognized as an internal or external command,
operable program or batch file.
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\richardg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\rpy2\situation.py", line 496, in <module>
    for row in iter_info():
  File "C:\Users\richardg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\rpy2\situation.py", line 454, in iter_info
    yield get_r_libnn(r_home)
          ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\richardg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\rpy2\situation.py", line 263, in get_r_libnn
    return _get_r_cmd_config(r_home, 'LIBnn',
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\richardg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\rpy2\situation.py", line 246, in _get_r_cmd_config
    output = subprocess.check_output(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1008.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 466, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1008.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '('C:\\Program Files\\R\\R-4.3.1\\bin\\x64\\R', 'CMD', 'config', 'LIBnn')' returned non-zero exit status 1.

What I've gathered from my futile attempts at fixing this is that 2 things here are important:

  1. error 0x7e loading R.dll Loading R library from rpy2: cannot load library 'C:\Program Files\R\R-4.3.1\bin\x64\R.dll': error 0x7e
  2. 'sh' is not recognized as an internal or external command, operable program or batch file.

What I've tried regarding 1. -

  • check if R.dll is really there (it is)
  • try running and using R itself (it works)

regarding 2. -

  • check if 'sh' not recognized is local rpy2 problem or global
    • it is rpy2 problem, ran this which was suggested and worked:
import ctypes

def main():
    print("Hello from Python!")

if __name__ == "__main__":
    main()

Solution

  • OSError: cannot load library 'gobject-2.0': error 0x7e

    Starting from Python 3.8 DLL dependencies for extension modules and DLLs loaded with ctypes on Windows are now resolved more securely. Only the system paths, the directory containing the DLL or PYD file, and directories added with add_dll_directory() are searched for load-time dependencies. Specifically, PATH and the current working directory are no longer used, and modifications to these will no longer have any effect on normal DLL resolution. If you followed the installation guide from the official documentation then the following example works.

    solving the 0x7e error seems to have made rpy2 functional and I didnt need to bother trying to fix the 'sh' error

    'sh' is not recognized as an internal or external command, operable program or batch file.