首页 文章

Python - py_compile没有输出

提问于
浏览
2

我只是检查是否创建了 .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” .

1 回答

  • 1

    独立文件中的字符串 aegsedrg (或任何其他非关键字字符串)不是语法错误 . 它可以是程序中其他地方定义的标识符 . py_compile 不执行编译的文件,无法捕获运行时错误(如 NameError ) .

    尝试在文件中编写语法错误的内容,例如相同的字符串后跟问号:

    py_compile.compile("test.py")
      File "test.py", line 1
        aegsedrg ?
                 ^
    SyntaxError: invalid syntax
    

相关问题