【问题标题】:Import python custom module when working with RStudio使用 RStudio 时导入 python 自定义模块
【发布时间】:2021-07-31 06:26:35
【问题描述】:

我使用来自 RStudio 的 Python 和 reticulate 包。是否可以像在普通 Python 中一样创建自定义模块并导入它们?这是一个 MWE:

我的自定义模块存储在一个名为 test_class.py 的文件中,并定义了 Test 类:

class Test:
  
  def __init__(self, name):
    self.name = name

我的主文件 main.pytest_class.py 位于同一目录中并包含

from test_class import Test

x = Test("Bobby")
print(x.name)

如果我使用 reticulate 在 RStudio 中运行主文件,则会失败并显示:ModuleNotFoundError: No module named 'test_class'。如果我使用 python (python main.py) 在终端中运行它,它会完美运行。如何在 RStudio 中获得这种行为?

【问题讨论】:

    标签: python class module rstudio reticulate


    【解决方案1】:

    我找到了罪魁祸首:reticulate 在项目目录中查找导入语句,而不是在进行导入的 python 文件的目录中。所以你需要在你的import语句中调整路径。

    【讨论】:

      猜你喜欢
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 2021-12-29
      相关资源
      最近更新 更多