Python - py_compile 没有输出

我正在尝试检查 python 代码中的错误,我需要访问错误日志本身(我不能只检查.pyc文件是否已创建)。


我目前的流程是我有一个要运行的脚本python3 -m py_compile test.py,其中test.py“aegsedrg”在哪里,没有别的(换句话说,无效的python代码)。


当 python3 test.py运行时,其结果是错误的(如预期):


Traceback (most recent call last):

  File "test.py", line 1, in <module>

    aegsedrg

NameError: name 'aegsedrg' is not defined

但是,python3 -m py_compile test.py返回(通过 stdout 或 stderr)没有错误。


这是在 Ubuntu 16.04 服务器上,我最近对其进行了升级和更新。我也完全重新安装了 Python 3。我认为这不是权限问题,test.py是 777。


为什么python3 -m py_compile test.py什么都不返回?


-编辑-


我还尝试使用以下 python 脚本来执行此操作:


import py_compile

print("Here")

py_compile.compile("test.py")

print("Here2")

输出是“Here”,然后是“Here2”。


狐的传说
浏览 364回答 1
1回答

四季花海

aegsedrg独立文件中的字符串本身(或任何其他非关键字字符串)不是语法错误。它可以是程序中其他地方定义的标识符。py_compile不执行已编译的文件,也无法捕获运行时错误(如NameError)。尝试在您的文件中编写一些语法错误的内容,例如相同的字符串后跟一个问号:py_compile.compile("test.py")&nbsp; File "test.py", line 1&nbsp; &nbsp; aegsedrg ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^SyntaxError: invalid syntax
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python