4

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".

1
  • @DYZ Sorry, forgot to include that I have tried that too. I'll update my question. Commented Dec 10, 2018 at 5:24

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.