[Python] syntax check for python?

horst horsu at freeshell.org
Fri Feb 2 11:48:16 PST 2007


> Date: Fri, 2 Feb 2007 09:31:17 -0800
> From: Bob Miller <kbob at jogger-egg.com>
> 
> Rob Hudson wrote:
>
>> Is there a way to syntax check a .py file without actually running it?
...>>
>> What does Python have?
>
>    $ python -c 'import myfile'
>

That's what I use. A few comments:

  a) It's of course 'myfile' and not myfile.py

  b) If it's for a web application, or other networked application that are 
ultimately run under a diffent userID than yours, you want to remove the 
.pyc file that was created by the import statement, and let the apache 
user recreate the .pyc file under that ownership. That way, whenever you 
change the .py file apache can recompile an updated .pyc because it ownes 
it. Otherwise it will be recompiled in memory everytime it's getting 
imported. It's only a minor performance issue; but it also makes 
diagnostic easier when you need to inspect access times (ls -lu) for 
trouble shooting.

  c) Some IDEs have indentation checks that give warnings if a .py contains 
mixed TAB and whitespace indentation. Such things happen when you work in 
a mixed environment where some developpers work strictly in vi using TABs 
and autoindent, and others just clip & paste (does TAB->space conversion 
on some terminals)-:
  Those IDEs, i think, use the tabnanny module... (see python docs)

  - Horst



More information about the Python mailing list