pepperjack
pepperjack

Reputation: 663

Python - py_compile has no output

I'm trying to check for errors in python code, and I need access to the error log itself (I can't just check to see if the .pyc file was created).

My current process is I have a script to run python3 -m py_compile test.py, where test.py is "aegsedrg" and nothing else (in other words, invalid python code).

When python3 test.py is run, the result is an error (as expected):

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    aegsedrg
NameError: name 'aegsedrg' is not defined

However, python3 -m py_compile test.py returns (through stdout or stderr) no errors.

This is on a Ubuntu 16.04 server, and I have recently upgraded and updated it. I have also completely reinstalled Python 3. I don't think this is a permission issue, test.py is 777.

Why does python3 -m py_compile test.py return nothing?

-edit-

I have also tried to use the following python script to do this:

import py_compile
print("Here")
py_compile.compile("test.py")
print("Here2")

The output is "Here" and then "Here2".

Upvotes: 4

Views: 2290

Answers (1)

DYZ
DYZ

Reputation: 57105

The string aegsedrg as such (or any other non-keyword string) in a standalone file is not a syntax error. It could be an identifier defined elsewhere in the program. py_compile does not execute the compiled file and cannot catch run-time errors (like NameError).

Try writing something syntactically incorrect in your file, such as the same string followed by a question mark:

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

Upvotes: 2

Related Questions