Compile python scripts without running them

Posted in python

If you want to check for syntax errors without actually running your code, you can compile it with py_compile. You may want to do this if your code modifies files or interacts with the network or external hardware or applications.

$ python -m py_compile first.py second.py third.py

I made an alias for it so I don’t have to type all that every time I want to compile without executing the code.

alias pmake=’python -m py_compile’

Here’s an error where the colon was left off the end of an if-statment:

  File "./snmpq.py", line 63
    if item[:1] == '"'
                     ^
SyntaxError: invalid syntax

The output looks a little different, but it gets the job done.

$ pmake snmpq.py
SyntaxError: ('invalid syntax', ('snmpq.py', 63, 35, '       if item[:1] == \'"\'\n'))
Posted by admica   @   7 July 2011

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

*

Previous Post
«
Next Post
»
Powered by Wordpress   |   Lunated designed by ZenVerse

Valid XHTML 1.0 Transitional