We help IT Professionals succeed at work.

compile Python

I am new to Python. I usually run a Python program by "python xxx.py". Nothing is generated in this way. How can I compile a Python program so that the compile errors could be shown?

BTW, I am using Python 2.5.2-3.
Comment
Watch Question

you can use
pychecker http://pychecker.sourceforge.net/
 pylint http://pypi.python.org/pypi/pylint


Both tools don't compile, but report errors and warn about things, which you might consider changing.
if all your code and your modules contain the

if __name__ ==  "__main__":
  your main code

then you could also just use python -m

example:
your file is called myprog.py

then just type

python -m myprog

Author

Commented:
gelonida, I tried "python -m xxx.py" and the Python program is without "if __name__ ==  "__main__":". A .pyc could still be generated.
concerning my first comment:
"Both tools don't compile,".
I meant. both tools do a little more than 'just compile'

Apart from that.


There is also a module named
compileall

if all your codde is below a certain directory tree you can do:
1.) create a small script compall.py following contents:
import compileall
compileall.compile_dir('.')

2.) call this script
Hi igotaquestion,


yes, the pyc is created, but all code will also be executed.

this might not always be your intention especially with programs running a long task
or performing tasks which you did not want to run for compling'


Just to show you what I mean:

create a fil with following line:
print "This could need very long or format your hard disk"


and type python -m filename # without the .py suffix.

you will see the printed out message (and not even create a .pyc)

using compileall as suggested in post 32977252 will compile 'without executng'
To compile only one file use
import py_compile
py_compile.compile('filename.py')

Author

Commented:
gelonida, what is the difference between "python -m test.py" and "python -m test"?
'm using python 2.6.4
With my python version the difference is rather simple.

python -m test
works if the file is called test.py

python -m test.py
doesn't and aborts with
No module named test.py

By the way:
I recommend to NOT use the filename test.py  , as 'test' is already a preexisting python package name.
If you don't use test you avoid potential conflicts and strange behaviour.