[英]Reposurgeon gives me a SyntaxError when using exec, why?
Using reposurgeon
and trying to extend its functionality, I am faced with: 使用reposurgeon
并尝试扩展其功能,我面临着:
reposurgeon: invalid syntax in extension function
which translates to a SyntaxError
extension raised from the execfile()
call in RepoSurgeon.do_exec()
. 转换为从RepoSurgeon.do_exec()
的execfile()
调用引发的SyntaxError
扩展名。 What gives? 是什么赋予了? The code I am trying to exec
is as simple as: 我尝试exec
的代码很简单:
print "Hello world"
I have also used the Python CLI and execfile
and there are no complaints whatsoever? 我也使用了Python CLI和execfile
,没有任何抱怨吗?
Used version: reposurgeon
3.10 使用的版本: reposurgeon
3.10
This one took me a while to figure out, which is why I am posting it here. 这花了我一段时间才弄清楚,这就是为什么我在这里发布它。
The key is indeed in the single line of code we're trying to "source". 关键确实在我们试图“获取”的单行代码中。 While this is perfectly valid Python 2.x code, reposurgeon
uses the print
function from Python 3.x by doing: 尽管这是完全有效的Python 2.x代码, reposurgeon
通过执行以下reposurgeon
使用了Python 3.x中的print
函数:
from __future__ import print_function
Which causes print
to require the use of parentheses, as it makes print
a function instead of a statement. 这导致print
需要使用括号,因为它使print
成为函数而不是语句。
Obviously we're running our extension code in the context of reposurgeon
, which means that we're dependent on the rules it defines. 显然,我们在reposurgeon
的上下文中运行扩展代码,这意味着我们依赖于它定义的规则。
See this document . 请参阅本文档 。
Hence the following will work just fine: 因此,以下将正常工作:
print("Hello world")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.