4

当尝试使用 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
4

1 回答 1

0

我还没有看到任何可靠的解决方案。在包中我做了这样的事情。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)
}
于 2019-08-20T01:20:48.673 回答