简体   繁体   English

Reposurgeon在使用exec时给了我SyntaxError,为什么?

[英]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.

相关问题 AppEngine exec字符串给出SyntaxError - AppEngine exec string gives SyntaxError 为什么使用!=运算符会在我的python程序中给我一个简单的错误? - Why is using the != operator gives me a simple error in my python program? 为什么它什么也没给我? - Why it just gives me nothing? 将exec()与pyqt一起使用时,为什么我的定义不见了? - Why are my definitions gone when using exec() with pyqt? 如何通过“ exec”和“ eval”替换(或扩展)RepoSurgeon的现有Python类方法? - How to replace existing Python class methods of (or otherwise extend) RepoSurgeon by means of 'exec' and 'eval'? 为什么 help(lambda) 给出 SyntaxError: invalid syntax - Why does help(lambda) gives SyntaxError: invalid syntax 尝试比较在Python 2.4中使用“ with open…”打开的文件会产生SyntaxError - Trying to compare files opened using 'with open…' in Python 2.4 gives a SyntaxError 当我从并发导入时,它给了我ImportError.futures import ThreadPoolExecutor`。 有人知道为什么吗? - It gives me ImportError when I import `from concurrent.futures import ThreadPoolExecutor`. Does anyone know why? 为什么python Requests lib在命令提示符下工作,但是在Django中实现时却给了我[Errno 11004]? - Why does python Requests lib work in command prompt, but gives me [Errno 11004] when implemented in Django? 为什么python dictonary给了我Keyerror? - Why python dictonary gives me Keyerror?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM