py vs pyc

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

mrmakent at wrote:

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


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

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

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

$ python2.4 <
$ python2.4 <q.pyc
sys:1: DeprecationWarning: Non-ASCII character '\xf2' in file <stdin> on line 1,
but no encoding declared; see for
  File "<stdin>", line 1
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


More information about the Python-list mailing list