【问题标题】:R reticulate package and python sub-modulesR网状包和python子模块
【发布时间】:2018-09-17 19:28:16
【问题描述】:

当尝试使用 R 的 reticulate 包模仿一些 python 代码时:

import_from_path("jwt_auth", "C:/Program Files/Python 3.5/Lib/site-packages/boxsdk/auth")

我收到此错误:

Error in py_module_import(module, convert = convert) : 
  SystemError: Parent module '' not loaded, cannot perform relative import

Detailed traceback: 
  File "C:\Program Files\Python 3.5\Lib\site-packages\boxsdk\auth\jwt_auth.py", line 13, in <module>
    from .oauth2 import OAuth2

有没有更好的方法在 R 中加载子模块,如下所示:

from boxsdk import JWTAuth

【问题讨论】:

    标签: r reticulate


    【解决方案1】:

    我还没有看到任何可靠的解决方案。在包中我做了这样的事情。有趣的是,reportlab 的下钻可以使用点符号来完成。

    # this is needed in case we use python in the R code rather than source_python
    fitz <- NULL
    pdfr <- NULL
    pdfw <- NULL
    repl <- NULL
    
    .onLoad <- function(libname, pkgname) {
      # this will fail in package check
      reticulate::use_condaenv(condaenv = "myenv", required = TRUE)
      fitz <<- reticulate::import("fitz", delay_load = TRUE)
      pdfr_0 <- reticulate::import("PyPDF2", delay_load = TRUE)
      pdfr <<- pdfr_0$PdfFileReader
      pdfw <<- pdfr_0$PdfFileWriter
      repl <- reticulate::import("reportlab.pdfgen.canvas", delay_load = TRUE)
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多