reticulate | R-python调用 | 安装及配置 | conda文件配置

reticulate | R-python安装及配置 | conda文件配置

  • [1. 基础知识](#1. 基础知识)
  • [2. 安装reticulate from CRAN](#2. 安装reticulate from CRAN)
  • [3. 包含了用于Python和R之间协同操作的全套工具,在R和Rstudio中均可使用](#3. 包含了用于Python和R之间协同操作的全套工具,在R和Rstudio中均可使用)
  • [4. 配置python环境](#4. 配置python环境)
  • [5. 在R中调用python模块-示例](#5. 在R中调用python模块-示例)
    • [5.1 调用os模块的listdir()函数](#5.1 调用os模块的listdir()函数)
    • [5.2 调用seaborn模块的load_dataset()函数](#5.2 调用seaborn模块的load_dataset()函数)
  • [6. 在 R 中安装 python 模块](#6. 在 R 中安装 python 模块)
  • [7. 重装miniconda后调试](#7. 重装miniconda后调试)
    • [7.1 R安装python包"rdkit"](#7.1 R安装python包"rdkit")
    • [7.2 R安装python包"pytorch"](#7.2 R安装python包"pytorch")
    • [7.3 R安装python包"seaborn"](#7.3 R安装python包"seaborn")
    • [7.4 R-python调用](#7.4 R-python调用)
  • [8. 总结](#8. 总结)

1. 基础知识

1.1 smiles编码具有唯一性:每个SMILES编码对应唯一一个化学结构

1.2 同一种格式的smiles编码的形式可以用不同的方式表示,起始原子序号不同,多种表达形式,可根据此特点来对数据进行数据增强;但在数据清洗筛选的过程中,会造成错误和麻烦,假如你从不同的数据库爬取了多条信息,可能出现同一个分子用多个smiles编码表示的情形,分子生成时也会出现类似状况现象,为避免这种状况,需要我们<对分子的smiles编码进行统一化,标准化>

2. 安装reticulate from CRAN

install.packages("reticulate")

3. 包含了用于Python和R之间协同操作的全套工具,在R和Rstudio中均可使用

library(reticulate)

4. 配置python环境

4.1 4种环境配置方式

use_python("/usr/local/bin/python")

Error in use_python("/usr/local/bin/python") :

Specified version of python '/usr/local/bin/python' does not exist.
use_python("D:/Program Files/Python312") #1. 直接指定使用python版本的执行程序

conda_version() #查看conda版本

use_condaenv() # 2.指定Python conda环境

use_virtualenv() # 3.指定Python虚拟环境

use_miniconda() # 4.指定Python miniconda环境
py_config() # 查看配置的环境

python: D:/Program Files/Python312/python.exe

libpython: D:/Program Files/Python312/python312.dll

pythonhome: D:/Program Files/Python312

version: 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)]

Architecture: 64bit

numpy: [NOT FOUND]

NOTE: Python version was forced by use_python() function

4.2 miniconda 环境

可通过以下代码安装 miniconda 环境,便可在 R 中执行 Python,辅助配置 Python 环境。

install_miniconda() # 安装miniconda环境
报错一:Error: Error creating conda environment 'r-reticulate' [exit code 1]

install_miniconda()报错一

在RStudio中配置miniconda环境报错,提示未创建conda环境,但运行以下代码后再次配置miniconda环境,显示正常,奇奇怪怪,所以很多时候多尝试一下总能行。
在R中调用Python中,安装reticulate包出现的环境设置错误

py_available()

[1] TRUE
conda_version() #查看conda版本

[1] "conda 23.9.0"
repl_python() #启动

Python 3.12.0 (D:/Program Files/Python312/python.exe)

Reticulate 1.34.0 REPL -- A Python interpreter in R.

Enter 'exit' or 'quit' to exit the REPL and return to R.

exit
install_miniconda() # 安装miniconda环境

Error: Miniconda is already installed at path "C:/Users/hp/AppData/Local/r-miniconda".

Use reticulate::install_miniconda(force = TRUE) to overwrite the previous installation.
miniconda_path() # 显示miniconda安装地址

[1] "C:/Users/hp/AppData/Local/r-miniconda"
miniconda_update() # 更新miniconda环境

failed

ProxyError: Conda cannot proceed due to an error in your proxy configuration.

Check for typos and other configuration errors in any '.netrc' file in your home directory,

any environment variables ending in '_PROXY', and any other system-wide proxy

configuration settings.

install_miniconda()报错二

第5节第6节调用numpy等包的时候,一直报错无法在R中调用,在CMD命令行中运行以下代码,未解决,并且导致后续miniconda运行代码一直报错,故卸载miniconda进行重装。

而利用install_miniconda()安装时,也遇到过以下问题,参照R-install_miniconda()卸载 | conda命令行报错及解决方法

报错二 :* Installing Miniconda -- please wait a moment ...* Downloading "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe" ...

trying URL 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe'

Warning: URL 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe': status was 'Couldn't resolve host name'Error in download.file(url, destfile = installer, mode = "wb") :

cannot open URL 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe'

解决方法参照上述方法卸载已安装的miniconda,关掉代理之后,再次运行该代码进行重新安装。

> install_miniconda() # 安装miniconda环境
* Installing Miniconda -- please wait a moment ...
* Downloading "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe" ...
trying URL 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe'
Content type 'application/octet-stream' length 77445248 bytes (73.9 MB)
downloaded 73.9 MB

+ "C:/Users/hp/AppData/Local/r-miniconda/condabin/conda.bat" update --yes --name base conda
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... done

## Package Plan ##

  environment location: C:\Users\hp\AppData\Local\R-MINI~1

  added / updated specs:
    - conda


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    archspec-0.2.1             |     pyhd3eb1b0_0          39 KB
    brotli-python-1.0.9        |  py311hd77b12b_7         310 KB
    conda-23.10.0              |  py311haa95532_0         1.2 MB
    conda-libmamba-solver-23.11.0|  py311haa95532_0         103 KB
    libmamba-1.5.3             |       hcd6fe79_0         3.8 MB
    libmambapy-1.5.3           |  py311h77c03ed_0         323 KB
    openssl-3.0.12             |       h2bbff1b_0         7.4 MB
    urllib3-1.26.18            |  py311haa95532_0         252 KB
    yaml-cpp-0.8.0             |       hd77b12b_0         2.0 MB
    ------------------------------------------------------------
                                           Total:        15.5 MB

The following NEW packages will be INSTALLED:

  archspec           pkgs/main/noarch::archspec-0.2.1-pyhd3eb1b0_0 
  brotli-python      pkgs/main/win-64::brotli-python-1.0.9-py311hd77b12b_7 

The following packages will be REMOVED:

  brotlipy-0.7.0-py311h2bbff1b_1002

The following packages will be UPDATED:

  conda                              23.9.0-py311haa95532_0 --> 23.10.0-py311haa95532_0 
  conda-libmamba-so~                 23.9.1-py311haa95532_0 --> 23.11.0-py311haa95532_0 
  libmamba                                 1.5.1-hcd6fe79_0 --> 1.5.3-hcd6fe79_0 
  libmambapy                          1.5.1-py311h77c03ed_0 --> 1.5.3-py311h77c03ed_0 
  openssl                                 3.0.11-h2bbff1b_2 --> 3.0.12-h2bbff1b_0 
  urllib3                           1.26.16-py311haa95532_0 --> 1.26.18-py311haa95532_0 
  yaml-cpp                                 0.7.0-hd77b12b_1 --> 0.8.0-hd77b12b_0 



Downloading and Extracting Packages: ...working... done
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
+ "C:/Users/hp/AppData/Local/r-miniconda/condabin/conda.bat" "create" "--yes" "--name" "r-reticulate" "python=3.9" "numpy" "--quiet" "-c" "conda-forge"
Channels:
 - conda-forge
 - defaults
Platform: win-64
Collecting package metadata (repodata.json): ...working... done
Solving environment: ...working... done

## Package Plan ##

  environment location: C:\Users\hp\AppData\Local\R-MINI~1\envs\r-reticulate

  added / updated specs:
    - numpy
    - python=3.9


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    bzip2-1.0.8                |       hcfcfb64_5         122 KB  conda-forge
    ca-certificates-2023.7.22  |       h56e8100_0         146 KB  conda-forge
    intel-openmp-2023.2.0      |   h57928b3_50497         2.4 MB  conda-forge
    libblas-3.9.0              |     19_win64_mkl         4.8 MB  conda-forge
    libcblas-3.9.0             |     19_win64_mkl         4.8 MB  conda-forge
    libffi-3.4.2               |       h8ffe710_5          41 KB  conda-forge
    libhwloc-2.9.3             |default_haede6df_1009         2.5 MB  conda-forge
    libiconv-1.17              |       h8ffe710_0         698 KB  conda-forge
    liblapack-3.9.0            |     19_win64_mkl         4.8 MB  conda-forge
    libsqlite-3.44.0           |       hcfcfb64_0         833 KB  conda-forge
    libxml2-2.11.5             |       hc3477c8_1         1.5 MB  conda-forge
    libzlib-1.2.13             |       hcfcfb64_5          54 KB  conda-forge
    mkl-2023.2.0               |   h6a75c08_50496       138.0 MB  conda-forge
    numpy-1.26.0               |   py39hddb5d58_0         5.7 MB  conda-forge
    openssl-3.1.4              |       hcfcfb64_0         7.1 MB  conda-forge
    pip-23.3.1                 |     pyhd8ed1ab_0         1.3 MB  conda-forge
    pthreads-win32-2.9.1       |       hfa6e2cd_3         141 KB  conda-forge
    python-3.9.18              |h4de0772_0_cpython        16.1 MB  conda-forge
    python_abi-3.9             |           4_cp39           7 KB  conda-forge
    setuptools-68.2.2          |     pyhd8ed1ab_0         454 KB  conda-forge
    tbb-2021.10.0              |       h91493d7_2         153 KB  conda-forge
    tk-8.6.13                  |       h5226925_1         3.3 MB  conda-forge
    tzdata-2023c               |       h71feb2d_0         115 KB  conda-forge
    ucrt-10.0.22621.0          |       h57928b3_0         1.2 MB  conda-forge
    vc-14.3                    |      h64f974e_17          17 KB  conda-forge
    vc14_runtime-14.36.32532   |      hdcecf7f_17         722 KB  conda-forge
    vs2015_runtime-14.36.32532 |      h05e6639_17          17 KB  conda-forge
    wheel-0.41.3               |     pyhd8ed1ab_0          57 KB  conda-forge
    xz-5.2.6                   |       h8d14728_0         213 KB  conda-forge
    ------------------------------------------------------------
                                           Total:       197.1 MB

The following NEW packages will be INSTALLED:

  bzip2              conda-forge/win-64::bzip2-1.0.8-hcfcfb64_5 
  ca-certificates    conda-forge/win-64::ca-certificates-2023.7.22-h56e8100_0 
  intel-openmp       conda-forge/win-64::intel-openmp-2023.2.0-h57928b3_50497 
  libblas            conda-forge/win-64::libblas-3.9.0-19_win64_mkl 
  libcblas           conda-forge/win-64::libcblas-3.9.0-19_win64_mkl 
  libffi             conda-forge/win-64::libffi-3.4.2-h8ffe710_5 
  libhwloc           conda-forge/win-64::libhwloc-2.9.3-default_haede6df_1009 
  libiconv           conda-forge/win-64::libiconv-1.17-h8ffe710_0 
  liblapack          conda-forge/win-64::liblapack-3.9.0-19_win64_mkl 
  libsqlite          conda-forge/win-64::libsqlite-3.44.0-hcfcfb64_0 
  libxml2            conda-forge/win-64::libxml2-2.11.5-hc3477c8_1 
  libzlib            conda-forge/win-64::libzlib-1.2.13-hcfcfb64_5 
  mkl                conda-forge/win-64::mkl-2023.2.0-h6a75c08_50496 
  numpy              conda-forge/win-64::numpy-1.26.0-py39hddb5d58_0 
  openssl            conda-forge/win-64::openssl-3.1.4-hcfcfb64_0 
  pip                conda-forge/noarch::pip-23.3.1-pyhd8ed1ab_0 
  pthreads-win32     conda-forge/win-64::pthreads-win32-2.9.1-hfa6e2cd_3 
  python             conda-forge/win-64::python-3.9.18-h4de0772_0_cpython 
  python_abi         conda-forge/win-64::python_abi-3.9-4_cp39 
  setuptools         conda-forge/noarch::setuptools-68.2.2-pyhd8ed1ab_0 
  tbb                conda-forge/win-64::tbb-2021.10.0-h91493d7_2 
  tk                 conda-forge/win-64::tk-8.6.13-h5226925_1 
  tzdata             conda-forge/noarch::tzdata-2023c-h71feb2d_0 
  ucrt               conda-forge/win-64::ucrt-10.0.22621.0-h57928b3_0 
  vc                 conda-forge/win-64::vc-14.3-h64f974e_17 
  vc14_runtime       conda-forge/win-64::vc14_runtime-14.36.32532-hdcecf7f_17 
  vs2015_runtime     conda-forge/win-64::vs2015_runtime-14.36.32532-h05e6639_17 
  wheel              conda-forge/noarch::wheel-0.41.3-pyhd8ed1ab_0 
  xz                 conda-forge/win-64::xz-5.2.6-h8d14728_0 


Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
* Miniconda has been successfully installed at "C:/Users/hp/AppData/Local/r-miniconda".
[1] "C:/Users/hp/AppData/Local/r-miniconda"

并且在R中利用py_config()查看python配置环境时,发现numpy包也不像4.1 4种环境配置方式中一样,显示为 [NOT FOUND],此处成功在R中调用numpy包。

利用miniconda_update()也可以正常运行,不像4.1 4种环境配置方式中显示ProxyError报错:

failed
ProxyError: Conda cannot proceed due to an error in your proxy configuration.

以上安装Miniconda6. 在 R 中安装 python 模块中安装python包报错问题类似,可参考。

总结报错问题-第5节及第6节报错问题也在此步之后解决】:

  1. 卸载重装Miniconda
  2. install_miniconda()报错二这一部分中,运行代码时,全程关闭代理
  3. 提前更新了hosts文件

5. 在R中调用python模块-示例

5.1 调用os模块的listdir()函数

os <- import("os")

os$listdir("./")

5.2 调用seaborn模块的load_dataset()函数

注:需要seaborn模块已安装

sns <- import("seaborn")

Error in py_module_import(module, convert = convert) :

ModuleNotFoundError: No module named 'seaborn'

Run reticulate::py_last_error() for details.
tips <- sns$load_dataset("tips")

print(head(tips))

6. 在 R 中安装 python 模块

6.1 安装seaborn绘图库

pip = T指定从pip安装,默认从conda安装

py_install("seaborn", pip = T)

"C:/Users/hp/AppData/Local/r-miniconda/condabin/conda.bat" "create" "--yes" "--name" "r-reticulate" "python" "--quiet" "-c" "conda-forge"

...

...

failed

ProxyError: Conda cannot proceed due to an error in your proxy configuration.

Check for typos and other configuration errors in any '.netrc' file in your home directory,

any environment variables ending in '_PROXY', and any other system-wide proxy

configuration settings.

Error: Error creating conda environment 'r-reticulate' [exit code 1]

安装又遇到了上述问题,难搞,继续解决bug!
在R中找不到python和conda

miniconda_path() # 显示miniconda安装地址

报错可能的原因
遇到报错:ProxyError: Conda cannot proceed due to an error in your proxy configuration

ProxyError解决方法及conda文件配置

  • 解决方法:
    关闭代理,重启RStudio,再次尝试安装,突然就安装成功了。

py_install("seaborn", pip = T)


Conda官网
Conda官网-Using the .condarc conda configuration file
conda配置文件修改
保留代理解决conda的ProxyError
解决conda ProxyError问题的一种思路
玩转python运行环境设置-conda配置文件修改
conda配置文件.condarc

  • 不关闭代理仍能解决conda的ProxyError方法:
    -- 在 第4.2节 安装 miniconda成功后,在电脑开始菜单栏,可以看到 Anaconda Prompt (r-miniconda)终端;

    -- 双击打开后,依次输入以下代码修改conda配置文件,添加本地代理地址信息
    -- conda --version,查看conda版本;

    -- conda info --envs,查看conda环境信息;

    -- conda config --show,查看conda的所有默认配置;

    -- conda config --show-sources,查看conda的.condarc配置文件位置,一般在(windows:C:\users\username\,linux:/home/username/ )目录下,默认情况下是不存在的,但在第一次运行conda config后会自动生成该文件;.condarc 默认是隐藏的,需要在查看里面,勾选隐藏的项目。也可以下载示例.condarc配置文件放置于用户目录下进行编辑并保存;


    -- conda info,查看.condarc配置文件信息,包括存储位置;

    -- 运行完成后,去 C:\Users(你的名字)\username\目录下可看到 .condarc 配置文件,可以用文本方式打开;


    -- .condarc配置文件有两种编辑方式:(1),利用conda config --set命令;(2),文本方式打开文件直接进行修改,具体参照玩转python运行环境设置-conda配置文件修改conda config或在终端中输入conda config --help查看其具体用法;

    -- 选择第1种方式------终端输入命令 进行演示:conda config --set show_channel_urls yes,该配置项指定了是否在 Conda 命令行输出中显示镜像源URL 。运行成功后,再次查看 .condarc配置文件,发现从空白增加了刚才运行的配置项;


    -- 添加国内镜像源 ,代码为conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgeconda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/;以上三行代码在终端中输入并运行;


    -- 添加中科大源 ,代码为conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/;以上六行代码在终端中输入并运行;
    -- 笔者本地代理地址(查询方法见:本地代理地址查看方法)为《127.0.0.1:7890 》,故需要将其配置给Conda,才能让 Conda 通过使用的代理连接互联网;


    -- 选择第2种方式------手动修改代理地址 ,从而实现保留代理,仍能够使用py_install在RStudio安装python包的目的。直接打开.condarc配置文件,输入以下内容并保存:
    proxy_servers: http: http://127.0.0.1:7890 https: http://127.0.0.1:7890
    以下图片依次对应 Anaconda Prompt (r-miniconda)终端输入代码运行结果

本地代理地址查看方法

R中python包无法加载调用的解决方法

查看seaborn模块是否已安装

py_module_available("seaborn")

[1] FALSE
加载rdkit.Chem和rdkit.Chem.rdmolops

rdkit <- import("rdkit.Chem")

Error in py_module_import(module, convert = convert) :

ModuleNotFoundError: No module named 'rdkit'

Run reticulate::py_last_error() for details.

以上通过R安装python包安装成功,却无法调用。

r-studio加载不出python模块pandas
Reticulate | 如何在Rstudio中优雅地调用Python
为什么在R和Python之间选择?
Using Python in R with reticulate package - Numpy not found
reticulate marks numpy as missing in Anaconda installation
已解决ModuleNotFoundError: No module named

解决方法参考链接

6.2 在创建环境中安装"numpy"包

创建新的工作环境

conda_create("r-reticulate")
在conda环境下安装"r-reticulate"和"numpy"

conda_install("r-reticulate","numpy")


以上代码可以在 Anaconda Prompt (r-miniconda)终端运行,也可以在win + r---CMD命令行终端运行。

7. 重装miniconda后调试

基于4.2 miniconda 环境中的install_miniconda()报错二进行重装后,在R中运行以下代码进行python包的安装和调用,发现都是成功的,表明已可以在R中调用python包。

7.1 R安装python包"rdkit"

reticulate::py_install("rdkit")

  • "C:/Users/hp/AppData/Local/r-miniconda/condabin/conda.bat" "install" "--yes" "--prefix" "C:/Users/hp/AppData/Local/r-miniconda/envs/r-reticulate" "-c" "conda-forge" "rdkit"
    Retrieving notices: ...working... done
    Channels: - conda-forge - defaults
    Platform: win-64
    Collecting package metadata (repodata.json): ...working... done
    Solving environment: ...working... done
    ##Package Plan ##
    environment location: C:\Users\hp\AppData\Local\r-miniconda\envs\r-reticulate
    added / updated specs:
    • rdkit
      The following packages will be downloaded:
      package | build
      ---------------------------|-----------------
      brotli-1.1.0 | hcfcfb64_1 19 KB conda-forge
      brotli-bin-1.1.0 | hcfcfb64_1 20 KB conda-forge
      cairo-1.18.0 | h1fef639_0 1.4 MB conda-forge
      certifi-2023.7.22 | pyhd8ed1ab_0 150 KB conda-forge
      contourpy-1.2.0 | py39h1f6ef14_0 182 KB conda-forge
      cycler-0.12.1 | pyhd8ed1ab_0 13 KB conda-forge
      expat-2.5.0 | h63175ca_1 221 KB conda-forge
      font-ttf-dejavu-sans-mono-2.37| hab24e00_0 388 KB conda-forge
      font-ttf-inconsolata-3.000 | h77eed37_0 94 KB conda-forge
      font-ttf-source-code-pro-2.038| h77eed37_0 684 KB conda-forge
      font-ttf-ubuntu-0.83 | hab24e00_0 1.9 MB conda-forge
      fontconfig-2.14.2 | hbde0cde_0 186 KB conda-forge
      fonts-conda-ecosystem-1 | 0 4 KB conda-forge
      fonts-conda-forge-1 | 0 4 KB conda-forge
      fonttools-4.44.0 | py39ha55989b_0 1.7 MB conda-forge
      freetype-2.12.1 | hdaf720e_2 498 KB conda-forge
      freetype-py-2.3.0 | pyhd8ed1ab_0 58 KB conda-forge
      gettext-0.21.1 | h5728263_0 5.3 MB conda-forge
      greenlet-3.0.1 | py39h99910a6_0 188 KB conda-forge
      icu-73.2 | h63175ca_0 12.8 MB conda-forge
      importlib-resources-6.1.1 | pyhd8ed1ab_0 9 KB conda-forge
      importlib_resources-6.1.1 | pyhd8ed1ab_0 29 KB conda-forge
      kiwisolver-1.4.5 | py39h1f6ef14_1 54 KB conda-forge
      lcms2-2.15 | h67d730c_3 487 KB conda-forge
      lerc-4.0.0 | h63175ca_0 190 KB conda-forge
      libboost-1.82.0 | h65993cd_6 2.3 MB conda-forge
      libboost-python-1.82.0 | py39h5c3ab46_6 108 KB conda-forge
      libbrotlicommon-1.1.0 | hcfcfb64_1 69 KB conda-forge
      libbrotlidec-1.1.0 | hcfcfb64_1 32 KB conda-forge
      libbrotlienc-1.1.0 | hcfcfb64_1 241 KB conda-forge
      libdeflate-1.19 | hcfcfb64_0 150 KB conda-forge
      libexpat-2.5.0 | h63175ca_1 135 KB conda-forge
      libglib-2.78.1 | he8f3873_0 2.5 MB conda-forge
      libjpeg-turbo-3.0.0 | hcfcfb64_1 804 KB conda-forge
      libpng-1.6.39 | h19919ed_0 336 KB conda-forge
      libtiff-4.6.0 | h6e2ebb7_2 769 KB conda-forge
      libwebp-base-1.3.2 | hcfcfb64_0 263 KB conda-forge
      libxcb-1.15 | hcd874cb_0 947 KB conda-forge
      m2w64-gcc-libgfortran-5.3.0| 6 342 KB conda-forge
      m2w64-gcc-libs-5.3.0 | 7 520 KB conda-forge
      m2w64-gcc-libs-core-5.3.0 | 7 214 KB conda-forge
      m2w64-gmp-6.1.0 | 2 726 KB conda-forge
      m2w64-libwinpthread-git-5.0.0.4634.697f757| 2 31 KB conda-forge
      matplotlib-base-3.8.1 | py39hf19769e_0 6.3 MB conda-forge
      msys2-conda-epoch-20160418 | 1 3 KB conda-forge
      munkres-1.1.4 | pyh9f0ad1d_0 12 KB conda-forge
      openjpeg-2.5.0 | h3d672ee_3 231 KB conda-forge
      packaging-23.2 | pyhd8ed1ab_0 48 KB conda-forge
      pandas-2.1.3 | py39h32e6231_0 10.7 MB conda-forge
      pcre2-10.40 | h17e33f8_0 1.9 MB conda-forge
      pillow-10.1.0 | py39h368b509_0 44.2 MB conda-forge
      pixman-0.42.2 | h63175ca_0 445 KB conda-forge
      pthread-stubs-0.4 | hcd874cb_1001 6 KB conda-forge
      pycairo-1.25.1 | py39h82915f6_0 101 KB conda-forge
      pyparsing-3.1.1 | pyhd8ed1ab_0 87 KB conda-forge
      python-dateutil-2.8.2 | pyhd8ed1ab_0 240 KB conda-forge
      python-tzdata-2023.3 | pyhd8ed1ab_0 140 KB conda-forge
      pytz-2023.3.post1 | pyhd8ed1ab_0 183 KB conda-forge
      rdkit-2023.09.2 | py39h4e49fef_0 34.6 MB conda-forge
      reportlab-4.0.7 | py39ha55989b_0 2.2 MB conda-forge
      rlpycairo-0.2.0 | pyhd8ed1ab_0 15 KB conda-forge
      six-1.16.0 | pyh6c4a22f_0 14 KB conda-forge
      sqlalchemy-2.0.23 | py39ha55989b_0 2.6 MB conda-forge
      typing-extensions-4.8.0 | hd8ed1ab_0 10 KB conda-forge
      typing_extensions-4.8.0 | pyha770c72_0 34 KB conda-forge
      unicodedata2-15.1.0 | py39ha55989b_0 365 KB conda-forge
      xorg-libxau-1.0.11 | hcd874cb_0 50 KB conda-forge
      xorg-libxdmcp-1.1.3 | hcd874cb_0 66 KB conda-forge
      zipp-3.17.0 | pyhd8ed1ab_0 19 KB conda-forge
      zlib-1.2.13 | hcfcfb64_5 105 KB conda-forge
      zstd-1.5.5 | h12be248_0 335 KB conda-forge

    Total: 141.8 MB
    The following NEW packages will be INSTALLED:
    brotli conda-forge/win-64::brotli-1.1.0-hcfcfb64_1
    brotli-bin conda-forge/win-64::brotli-bin-1.1.0-hcfcfb64_1
    cairo conda-forge/win-64::cairo-1.18.0-h1fef639_0
    certifi conda-forge/noarch::certifi-2023.7.22-pyhd8ed1ab_0
    contourpy conda-forge/win-64::contourpy-1.2.0-py39h1f6ef14_0
    cycler conda-forge/noarch::cycler-0.12.1-pyhd8ed1ab_0
    expat conda-forge/win-64::expat-2.5.0-h63175ca_1
    font-ttf-dejavu-s~ conda-forge/noarch::font-ttf-dejavu-sans-mono-2.37-hab24e00_0
    font-ttf-inconsol~ conda-forge/noarch::font-ttf-inconsolata-3.000-h77eed37_0
    font-ttf-source-c~ conda-forge/noarch::font-ttf-source-code-pro-2.038-h77eed37_0
    font-ttf-ubuntu conda-forge/noarch::font-ttf-ubuntu-0.83-hab24e00_0
    fontconfig conda-forge/win-64::fontconfig-2.14.2-hbde0cde_0
    fonts-conda-ecosy~ conda-forge/noarch::fonts-conda-ecosystem-1-0
    fonts-conda-forge conda-forge/noarch::fonts-conda-forge-1-0
    fonttools conda-forge/win-64::fonttools-4.44.0-py39ha55989b_0
    freetype conda-forge/win-64::freetype-2.12.1-hdaf720e_2
    freetype-py conda-forge/noarch::freetype-py-2.3.0-pyhd8ed1ab_0
    gettext conda-forge/win-64::gettext-0.21.1-h5728263_0
    greenlet conda-forge/win-64::greenlet-3.0.1-py39h99910a6_0
    icu conda-forge/win-64::icu-73.2-h63175ca_0
    importlib-resourc~ conda-forge/noarch::importlib-resources-6.1.1-pyhd8ed1ab_0
    importlib_resourc~ conda-forge/noarch::importlib_resources-6.1.1-pyhd8ed1ab_0
    kiwisolver conda-forge/win-64::kiwisolver-1.4.5-py39h1f6ef14_1
    lcms2 conda-forge/win-64::lcms2-2.15-h67d730c_3
    lerc conda-forge/win-64::lerc-4.0.0-h63175ca_0
    libboost conda-forge/win-64::libboost-1.82.0-h65993cd_6
    libboost-python conda-forge/win-64::libboost-python-1.82.0-py39h5c3ab46_6
    libbrotlicommon conda-forge/win-64::libbrotlicommon-1.1.0-hcfcfb64_1
    libbrotlidec conda-forge/win-64::libbrotlidec-1.1.0-hcfcfb64_1
    libbrotlienc conda-forge/win-64::libbrotlienc-1.1.0-hcfcfb64_1
    libdeflate conda-forge/win-64::libdeflate-1.19-hcfcfb64_0
    libexpat conda-forge/win-64::libexpat-2.5.0-h63175ca_1
    libglib conda-forge/win-64::libglib-2.78.1-he8f3873_0
    libjpeg-turbo conda-forge/win-64::libjpeg-turbo-3.0.0-hcfcfb64_1
    libpng conda-forge/win-64::libpng-1.6.39-h19919ed_0
    libtiff conda-forge/win-64::libtiff-4.6.0-h6e2ebb7_2
    libwebp-base conda-forge/win-64::libwebp-base-1.3.2-hcfcfb64_0
    libxcb conda-forge/win-64::libxcb-1.15-hcd874cb_0
    m2w64-gcc-libgfor~ conda-forge/win-64::m2w64-gcc-libgfortran-5.3.0-6
    m2w64-gcc-libs conda-forge/win-64::m2w64-gcc-libs-5.3.0-7
    m2w64-gcc-libs-co~ conda-forge/win-64::m2w64-gcc-libs-core-5.3.0-7
    m2w64-gmp conda-forge/win-64::m2w64-gmp-6.1.0-2
    m2w64-libwinpthre~ conda-forge/win-64::m2w64-libwinpthread-git-5.0.0.4634.697f757-2
    matplotlib-base conda-forge/win-64::matplotlib-base-3.8.1-py39hf19769e_0
    msys2-conda-epoch conda-forge/win-64::msys2-conda-epoch-20160418-1
    munkres conda-forge/noarch::munkres-1.1.4-pyh9f0ad1d_0
    openjpeg conda-forge/win-64::openjpeg-2.5.0-h3d672ee_3
    packaging conda-forge/noarch::packaging-23.2-pyhd8ed1ab_0
    pandas conda-forge/win-64::pandas-2.1.3-py39h32e6231_0
    pcre2 conda-forge/win-64::pcre2-10.40-h17e33f8_0
    pillow conda-forge/win-64::pillow-10.1.0-py39h368b509_0
    pixman conda-forge/win-64::pixman-0.42.2-h63175ca_0
    pthread-stubs conda-forge/win-64::pthread-stubs-0.4-hcd874cb_1001
    pycairo conda-forge/win-64::pycairo-1.25.1-py39h82915f6_0
    pyparsing conda-forge/noarch::pyparsing-3.1.1-pyhd8ed1ab_0
    python-dateutil conda-forge/noarch::python-dateutil-2.8.2-pyhd8ed1ab_0
    python-tzdata conda-forge/noarch::python-tzdata-2023.3-pyhd8ed1ab_0
    pytz conda-forge/noarch::pytz-2023.3.post1-pyhd8ed1ab_0
    rdkit conda-forge/win-64::rdkit-2023.09.2-py39h4e49fef_0
    reportlab conda-forge/win-64::reportlab-4.0.7-py39ha55989b_0
    rlpycairo conda-forge/noarch::rlpycairo-0.2.0-pyhd8ed1ab_0
    six conda-forge/noarch::six-1.16.0-pyh6c4a22f_0
    sqlalchemy conda-forge/win-64::sqlalchemy-2.0.23-py39ha55989b_0
    typing-extensions conda-forge/noarch::typing-extensions-4.8.0-hd8ed1ab_0
    typing_extensions conda-forge/noarch::typing_extensions-4.8.0-pyha770c72_0
    unicodedata2 conda-forge/win-64::unicodedata2-15.1.0-py39ha55989b_0
    xorg-libxau conda-forge/win-64::xorg-libxau-1.0.11-hcd874cb_0
    xorg-libxdmcp conda-forge/win-64::xorg-libxdmcp-1.1.3-hcd874cb_0
    zipp conda-forge/noarch::zipp-3.17.0-pyhd8ed1ab_0
    zlib conda-forge/win-64::zlib-1.2.13-hcfcfb64_5
    zstd conda-forge/win-64::zstd-1.5.5-h12be248_0
    Downloading and Extracting Packages: ...working... done
    Preparing transaction: ...working... done
    Verifying transaction: ...working... done
    Executing transaction: ...working... done
    import("rdkit")

Module(rdkit)



7.2 R安装python包"pytorch"

可以直接安装R包torch------R语言深度学习知多少-torch

> reticulate::py_install("pytorch")
+ "C:/Users/hp/AppData/Local/r-miniconda/condabin/conda.bat" "install" "--yes" "--prefix" "C:/Users/hp/AppData/Local/r-miniconda/envs/r-reticulate" "-c" "conda-forge" "pytorch"
Channels:
 - conda-forge
 - defaults
Platform: win-64
Collecting package metadata (repodata.json): ...working... done
Solving environment: ...working... done

## Package Plan ##

  environment location: C:\Users\hp\AppData\Local\r-miniconda\envs\r-reticulate

  added / updated specs:
    - pytorch


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    blas-1.0                   |              mkl           1 KB  conda-forge
    cffi-1.16.0                |   py39ha55989b_0         231 KB  conda-forge
    filelock-3.13.1            |     pyhd8ed1ab_0          15 KB  conda-forge
    jinja2-3.1.2               |     pyhd8ed1ab_1          99 KB  conda-forge
    libuv-1.44.2               |       hcfcfb64_1         278 KB  conda-forge
    markupsafe-2.1.3           |   py39ha55989b_1          26 KB  conda-forge
    mkl-service-2.4.0          |   py39h2bbff1b_1          45 KB
    mpmath-1.3.0               |     pyhd8ed1ab_0         428 KB  conda-forge
    networkx-3.2.1             |     pyhd8ed1ab_0         1.1 MB  conda-forge
    ninja-1.11.1               |       h91493d7_0         273 KB  conda-forge
    pycparser-2.21             |     pyhd8ed1ab_0         100 KB  conda-forge
    pytorch-2.0.1              |cpu_py39hb0bdfb8_0        88.8 MB
    sympy-1.12                 |     pyh04b8f61_3         4.0 MB  conda-forge
    ------------------------------------------------------------
                                           Total:        95.4 MB

The following NEW packages will be INSTALLED:

  blas               conda-forge/win-64::blas-1.0-mkl 
  cffi               conda-forge/win-64::cffi-1.16.0-py39ha55989b_0 
  filelock           conda-forge/noarch::filelock-3.13.1-pyhd8ed1ab_0 
  jinja2             conda-forge/noarch::jinja2-3.1.2-pyhd8ed1ab_1 
  libuv              conda-forge/win-64::libuv-1.44.2-hcfcfb64_1 
  markupsafe         conda-forge/win-64::markupsafe-2.1.3-py39ha55989b_1 
  mkl-service        pkgs/main/win-64::mkl-service-2.4.0-py39h2bbff1b_1 
  mpmath             conda-forge/noarch::mpmath-1.3.0-pyhd8ed1ab_0 
  networkx           conda-forge/noarch::networkx-3.2.1-pyhd8ed1ab_0 
  ninja              conda-forge/win-64::ninja-1.11.1-h91493d7_0 
  pycparser          conda-forge/noarch::pycparser-2.21-pyhd8ed1ab_0 
  pytorch            pkgs/main/win-64::pytorch-2.0.1-cpu_py39hb0bdfb8_0 
  sympy              conda-forge/noarch::sympy-1.12-pyh04b8f61_3 



Downloading and Extracting Packages: ...working... done
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done


但调用时显示缺少"pytorch"组件,重新安装又提示已安装,如下:

reticulate::py_install("pytorch")

  • "C:/Users/hp/AppData/Local/r-miniconda/condabin/conda.bat" "install" "--yes" "--prefix" "C:/Users/hp/AppData/Local/r-miniconda/envs/r-reticulate" "-c" "conda-forge" "pytorch"
    Channels:
    -conda-forge
    -defaults
    Platform: win-64
    Collecting package metadata (repodata.json): ...working... done
    Solving environment: ...working... done
    All requested packages already installed.
    py_module_available("pytorch")

[1] FALSE
import("pytorch")

Error in py_module_import(module, convert = convert) :

ModuleNotFoundError: No module named 'pytorch'

Run reticulate::py_last_error() for details.

原因及解决办法
ModuleNotFoundError: 未找到名为"No_module_named"的模块的通俗解释和解决方法
ModuleNotFoundError: No module named 'xxx'可能的解决方案大全
python通过pip安装torch错误及解决办法ERROR: Could not find a version that satisfies the requirement torch

通过CMD命令行提示可知,直接pip install安装无法找到合适的版本,故需要从官网下载后安装。

在官网查到,PyTorch在windows上仅支持3.8-3.11的python版本,而笔者电脑上是3.12版本,故版本不匹配,无法安装或调用。

具体的解决办法,以后用到再说吧...

7.3 R安装python包"seaborn"

reticulate::py_install("seaborn")

  • "C:/Users/hp/AppData/Local/r-miniconda/condabin/conda.bat" "install" "--yes" "--prefix" "C:/Users/hp/AppData/Local/r-miniconda/envs/r-reticulate" "-c" "conda-forge" "seaborn"
    Channels:
    conda-forge
    defaults
    Platform: win-64
    Collecting package metadata (repodata.json): ...working... done
    Solving environment: ...working... done
    ##Package Plan ##
    environment location: C:\Users\hp\AppData\Local\r-miniconda\envs\r-reticulate
    added / updated specs:
    • seaborn
      The following packages will be downloaded:
      package | build
      ---------------------------|-----------------
      patsy-0.5.3 | pyhd8ed1ab_0 189 KB conda-forge
      scipy-1.11.3 | py39hddb5d58_1 13.5 MB conda-forge
      seaborn-0.13.0 | hd8ed1ab_0 7 KB conda-forge
      seaborn-base-0.13.0 | pyhd8ed1ab_0 228 KB conda-forge
      statsmodels-0.14.0 | py39hd88c2e4_2 9.5 MB conda-forge

    Total: 23.5 MB
    The following NEW packages will be INSTALLED:
    patsy conda-forge/noarch::patsy-0.5.3-pyhd8ed1ab_0
    scipy conda-forge/win-64::scipy-1.11.3-py39hddb5d58_1
    seaborn conda-forge/noarch::seaborn-0.13.0-hd8ed1ab_0
    seaborn-base conda-forge/noarch::seaborn-base-0.13.0-pyhd8ed1ab_0
    statsmodels conda-forge/win-64::statsmodels-0.14.0-py39hd88c2e4_2
    Downloading and Extracting Packages: ...working... done
    Preparing transaction: ...working... done
    Verifying transaction: ...working... done
    Executing transaction: ...working... done
    py_module_available("seaborn")

[1] TRUE
import("seaborn")

Module(seaborn)

7.4 R-python调用

Sys.which("python")

python

"C:\Users\hp\AppData\Local\R-MINI~1\envs\R-RETI~1\python.exe"
load python pkgs in R
调用os模块的listdir()函数

os <- import("os")

os$listdir("./")

[1] ".Rhistory" "200205-R包randomForest的随机森林分类模型以及对重要变量的选择"

[3] "200627-R包randomForest的随机森林回归模型以及对重要变量的选择" "210117-如何评估随机森林模型以及重要预测变量的显著性"

[5] "mastering-software-development-in-r.pdf" "rmarkdown-cheatsheet.pdf"

[7] "Rmarkdown入门教程-庄闪闪" "rstudio_ide_commands.pdf"

[9] "R_functions.R" "R_install.R"

[11] "R_packages" "R_ZJH"

[13] "R语言可视化教程---庄闪闪" "R语言:从数据思维到数据实战-数据代码-狗熊会-朱雪宁"
import("os")

Module(os)
import("numpy")

Module(numpy)
reticulate::py_available()

[1] TRUE
import("rdkit")

Error in py_module_import(module, convert = convert) :

ModuleNotFoundError: No module named 'rdkit'

Run reticulate::py_last_error() for details.

8. 总结

以上所有报错解决办法总结:

  1. 卸载重装Miniconda
  2. install_miniconda()报错二这一部分中,运行代码时,全程关闭代理
  3. 提前更新了hosts文件

其他学习资料

Install Miniconda
Install Miniconda In rstudio/reticulate: Interface to 'Python'
Urgent! How can i install miniconda under R environment by using reticulate?
unable to install miniconda in R

github-reticulate包
官网介绍-R Interface to Python
reticulate:在R中使用Python
手把手 | 哇!用R也可以跑Python了

reticulate-Calling Python from R
reticulate-Installing Python Packages
Installing and Configuring Python with RStudio

相关推荐
湫ccc1 小时前
《Python基础》之字符串格式化输出
开发语言·python
mqiqe2 小时前
Python MySQL通过Binlog 获取变更记录 恢复数据
开发语言·python·mysql
AttackingLin2 小时前
2024强网杯--babyheap house of apple2解法
linux·开发语言·python
哭泣的眼泪4082 小时前
解析粗糙度仪在工业制造及材料科学和建筑工程领域的重要性
python·算法·django·virtualenv·pygame
湫ccc2 小时前
《Python基础》之基本数据类型
开发语言·python
drebander3 小时前
使用 Java Stream 优雅实现List 转化为Map<key,Map<key,value>>
java·python·list
威威猫的栗子4 小时前
Python Turtle召唤童年:喜羊羊与灰太狼之懒羊羊绘画
开发语言·python
墨染风华不染尘4 小时前
python之开发笔记
开发语言·笔记·python
Dxy12393102165 小时前
python bmp图片转jpg
python
麦麦大数据5 小时前
Python棉花病虫害图谱系统CNN识别+AI问答知识neo4j vue+flask深度学习神经网络可视化
人工智能·python·深度学习