简体   繁体   English

我应该如何最好地在 RStudio 中“运行”Python 脚本? 通过运行(使用 reticulate::repl_python()?)或来源?

[英]How should I preferably "run" Python scripts in RStudio? Through run (using reticulate::repl_python()?) or source?

I just started to use RStudio with Python (up to now everything works) and I wonder if there is a preferred way to run scripts such as my small Test.py containing我刚开始使用 RStudio 和 Python(到目前为止一切正常),我想知道是否有更好的方式来运行脚本,例如我的小Test.py包含

import inspect
import os

a = 5
print(a)

print(inspect.currentframe())
os.chdir("C:/.../") # a path, whatever you like
cwd = os.getcwd()
 
# print the current directory
print("Current working directory is:", cwd)

There is an answer for R here but this does not consider Python. I tried the flollowing:此处有 R 的答案,但这不考虑 Python。我尝试了以下操作:

  1. Source (Ctrl + Shift + S) from R-console => R-console is used来自 R-console 的源 (Ctrl + Shift + S) => 使用 R-console

在此处输入图像描述

  1. Start Python console using reticulate::repl_python() in R-console => Change from R- to Python-console在 R-console 中使用reticulate::repl_python()启动 Python 控制台 => 从 R- 更改为 Python-console

在此处输入图像描述

  1. Source (Ctrl + Shift + S) from Python-console => Change from Python- to R-console来自 Python 控制台的源代码 (Ctrl + Shift + S) => 从 Python 控制台更改为 R 控制台

在此处输入图像描述

It seems, reticulate::repl_python() forces the quit first.似乎reticulate::repl_python()首先强制quit

  1. Run lines or a selection using "run" (Ctrl + Enter) => Starts Python-console and stays there使用“运行”(Ctrl + Enter)运行行或选择=>启动Python控制台并停留在那里

在此处输入图像描述


This reference for reticulate and its description did not help : The reticulate package provides a comprehensive set of tools for interoperability between Python and R... Reticulate embeds a Python session within your R session, enabling seamless, high-performance interoperability. This reference for reticulate and its description did not help : The reticulate package provides a comprehensive set of tools for interoperability between Python and R... Reticulate embeds a Python session within your R session, enabling seamless, high-performance interoperability.

In the RStudio IDE there should be a "Source script" button (keyboard shortcut on a Mac: shift-cmd-S) which sources the script.在 RStudio IDE 中应该有一个“源脚本”按钮(Mac 上的键盘快捷键:shift-cmd-S),用于获取脚本。

在此处输入图像描述

From the terminal this is then how it looks:从终端看,这就是它的外观:

reticulate::source_python('~/Untitled.py')

(Too long for a comment. Maybe it helps others and hopefully, everything is correct) (评论太长了。也许它可以帮助别人,希望一切都是正确的)

What I understand now, RStudio provides an editor for R (not surprising).据我所知,RStudio 为 R 提供了一个编辑器(不足为奇)。 When you run Python code within RStudio, there is an R-wrapper for Python translating the code to R.当您在 RStudio 中运行 Python 代码时,有一个 Python 的 R-wrapper 将代码转换为 R。

Thus, RStudio does not switch the editor to a native Python-IDE (which was my expectation) like VScode or others.因此,RStudio 不会将编辑器切换到原生 Python-IDE(这是我的期望),如 VScode 或其他。

What I understand now: If you have projects with Python only, use a Python editor.我现在的理解是:如果您只有 Python 的项目,请使用 Python 编辑器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM