【问题标题】:knitr won't run Python commandsknitr 不会运行 Python 命令
【发布时间】:2014-05-21 21:37:46
【问题描述】:

knitr我用了很久,一般是在R Studio环境下。最近在我的windows机器上安装了python(3.4.1版本),放到path下,试用了谢易慧的sample document for Python。但是 Python 代码块不会运行。从这样的块中:

{r test-python, engine='python'}
x = 'hello, python world!'
print x
print x.split(' ')

我收到如下错误消息:

Warning: running command '"python"  -c "print '**Write** _something_ in `Markdown` from `Python`!'"' had status 1
running: "python"  -c "x = 'hello, python world!'
print x
print x.split(' ')"
  File "<string>", line 2
    print x
          ^
SyntaxError: invalid syntax

我在 Windows 7 中,运行 R 3.1.0,RStudio 版本 0.98.847(测试预览版)。交互式 Python 可以从命令行很好地打开。

有什么想法吗?

【问题讨论】:

    标签: r python-3.x knitr


    【解决方案1】:

    您的问题是您已经安装了 python3,但您使用的语法是 python2。 py2 -> py3 转换涉及对语言本身的更改-在您的示例中,print 已从语法更改为函数。 (所以print(x) 可以在你上面的代码中使用。)

    最简单的选择是卸载 python3 并安装最新的 Python 2.7(当前为 Python 2.7.6)。或者,向前和向上 - 使用 py3,它只涉及可能调整您在 knitr 中遇到的任何现有示例。

    【讨论】:

      猜你喜欢
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 2022-11-18
      • 2016-05-30
      • 2021-02-06
      • 1970-01-01
      相关资源
      最近更新 更多