Resolving rpy2 Installation Permission Issues via Wheel Files

问题背景

最近在尝试Python与R语言交互时,需要安装rpy2这个包。按照常规方式使用pip安装时,却不断遇到各种各样的错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
        File "Z:\ProgramData\miniconda3\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

还有其他各种各样的问题,在重新设置 R_HOME (https://www.cnblogs.com/Xeonilian/p/windows_rpy2_install.html) 、安装 R_Tools (https://blog.csdn.net/BioFlorist/article/details/121665959) 、安装 MinGW 等等方法都无法解决。
这个问题困扰了我整整两天,尝试了各种方法都未能解决。
甚至到了我一度以为是rpy2不支持windows(在rpy2的官网上,他说对windows的支持不由官方提供,而是由社区提供),但是我同学却轻轻松松安装成功了。

解决方案

在多次尝试失败后,网上别人推荐使用conda安装的方法,但是那种虽然能安装,但是却使用的是conda安装的R环境,而不是我本地的R环境。有大量的R包需要重装。
在找了许久之后,我发现可以通过预编译的wheel文件来绕过编译问题。
但是,网上很多教程用的是托管在www.lfd.uci.edu的wheel包,但是维护托管网站的教授因为服务器费用问题,已经关闭不再提供对rpy2的托管。
不过后来经过寻找,我在piwheels网站上找到了预编译好的rpy2 wheel文件。
虽然没有很多版本的wheel文件,最新的稳定版3.5.17也没有,只有3.6.0.dev2 pre-release,但是怎么说都比没有强很多了。

另外,官网上也有些R和rpy2的兼容性情况

安装步骤

  1. 访问piwheels
  2. 根据你的python版本,找到对应的wheel文件
  3. 下载wheel文件到本地
  4. 打开命令行,使用以下命令安装:
1
pip install 你下载的wheel文件路径

安装要点

安装前请确保系统中已经正确配置了R语言环境,rpy2需要调用R的库文件。

总结

当pip编译rpy2出现错误时,可以去www.piwheels.org试试看,能不能搜到预编译的wheel文件。
虽然这个过程有点曲折,但希望我的经历能帮助到遇到类似问题的你,少走一些弯路。

Problem Background

Recently, while attempting Python and R language interaction, I needed to install the rpy2 package. When installing via pip in the usual way, I kept encountering various errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
        File "Z:\ProgramData\miniconda3\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

There were also various other issues. Methods like resetting R_HOME (https://www.cnblogs.com/Xeonilian/p/windows_rpy2_install.html), installing R_Tools (https://blog.csdn.net/BioFlorist/article/details/121665959), and installing MinGW all failed to resolve the problem.
This issue plagued me for two full days, and none of the methods I tried could fix it.
I even wondered if rpy2 simply didn’t support Windows (on the rpy2 official website, it states that Windows support is not provided by the official team but by the community), but my classmate installed it effortlessly.

Solution

After multiple failed attempts, some online sources recommended using conda for installation. While that method could install it, it would use the R environment installed by conda rather than my local R environment, requiring reinstalling a large number of R packages.
After searching for a long time, I found that using precompiled wheel files could bypass the compilation issues.
However, many online tutorials used wheel packages hosted on www.lfd.uci.edu, but the professor maintaining that hosting site had shut it down and no longer provided hosting for rpy2 due to server costs.
Later, after further searching, I found precompiled rpy2 wheel files on the piwheels website.
Although there aren’t many version options available — the latest stable version 3.5.17 isn’t there, only the 3.6.0.dev2 pre-release — it’s still much better than having nothing.

Also, the official website has some information on R and rpy2 compatibility.

Installation Steps

  1. Visit piwheels
  2. Find the corresponding wheel file based on your Python version.
  3. Download the wheel file locally.
  4. Open a command line and install using the following command:
1
pip install path_to_your_downloaded_wheel_file

Key Installation Points

Ensure your system has a correctly configured R language environment before installation, as rpy2 needs to call R library files.

Conclusion

When pip compilation errors occur with rpy2, you can try visiting www.piwheels.org to see if precompiled wheel files are available for download.
Although this process was somewhat circuitous, I hope my experience can help you if you encounter similar issues, saving you some detours.


Resolving rpy2 Installation Permission Issues via Wheel Files
https://tokisaki.top/blog/rpy2-install-via-wheel/
作者
Tokisaki Galaxy
发布于
2025年2月27日
许可协议