py vs pyc

Fredrik Lundh fredrik at pythonware.com
Mon Jan 9 09:44:42 EST 2006


mrmakent at cox.net wrote:

>> what is the difference between runing from cmd  "python test.py" and
>> "python test.pyc"
>
> When you run 'python test.py', the python interpreter first looks to
> see if 'test.pyc' (which is the byte-code compiled version of
> 'test.py') exists, and if it is more recent than 'test.py'.

nope.

the import statement does this, but the Python interpreter doesn't (running
test.py as a script isn't the same thing as importing it as a module).  the inter-
preter does recognize a compiled file, though:

$ cat >q.py
print "hello"
$ python2.4 q.py
hello
$ ls q.pyc
ls: q.pyc: No such file or directory
$ python2.4 -c "import q"
hello
$ ls q.pyc
q.pyc
$ python2.4 q.pyc
hello

however, this only works if you're passing in a filename:

$ python2.4 <q.py
hello
$ python2.4 <q.pyc
sys:1: DeprecationWarning: Non-ASCII character '\xf2' in file <stdin> on line 1,
but no encoding declared; see http://www.python.org/peps/pep-0263.html for
details
  File "<stdin>", line 1
    m=
     ^
SyntaxError: invalid syntax

and it usually won't work if you use another Python version:

$ python2.3 q.pyc
RuntimeError: Bad magic number in .pyc file

</F> 






More information about the Python-list mailing list