Обложка канала

Библиотека Python разработчика

20835 @BookPython

Библиотека Python разработчика. Книги по программированию на Python.

Библиотека Python разработчика

4 года назад
Открыть в
Sometimes you want to check the syntax of a py-file without running it. Such naive check may be useful as a commit-hook or a fast continuous integration check. There is no direct way to do this. You can run the file as python -m module.py, that prevents the traditional if __name__ == '__main__' block from running. Still, all imports will be executed, and this may fail if you want to check syntax in the environment where the module can't be and shouldn't be run. However, the python standard library contains the py_compile module that generates byte-code from Python source file without running it. That's exactly what we need: $ python -m py_compile test.c File "test.c", line 1 int main() { ^ SyntaxError: invalid syntax.