How can I check the syntax of Python script without executing it?

前端 未结 8 1041
长发绾君心
长发绾君心 2020-11-29 14:17

I used to use perl -c programfile to check the syntax of a Perl program and then exit without executing it. Is there an equivalent way to do this for a Python s

相关标签:
8条回答
  • 2020-11-29 15:22

    Perhaps useful online checker PEP8 : http://pep8online.com/

    0 讨论(0)
  • 2020-11-29 15:22

    Thanks to the above answers @Rosh Oxymoron. I improved the script to scan all files in a dir that are python files. So for us lazy folks just give it the directory and it will scan all the files in that directory that are python.

    import sys
    import glob, os
    
    os.chdir(sys.argv[1])
    for file in glob.glob("*.py"):
        source = open(file, 'r').read() + '\n'
        compile(source, file, 'exec')
    

    Save this as checker.py and run python checker.py ~/YOURDirectoryTOCHECK

    0 讨论(0)
提交回复
热议问题