How to check if two .pyc files are the same byte code

Question: I have two .pyc Python bytecode files that I obtained from somewhere. Is there an easy way to check if these two .pyc files contain the same byte code?

Since .pyc files are in binary format, you may attempt to either compare their md5sum outputs, or use cmp command to do binary diff. However, using these tools will not work for .pyc files as is, because .pyc files contain extra metadata in them, which may be different even when the byte code itself is identical.

More specifically, the first 4 bytes of a .pyc file store a "magic" number which identifies the version of Python used to compile .pyc file. The next 4 bytes contain the timestamp of the Python source file.

Therefore, what you can do is to ignore the first 8 bytes of .pyc files, and compare the rest of the files. This can be achieved by the following command.

$ cmp <(tail -c +8 file.pyc) <(tail -c +8 file2.pyc)

Note that this command works for bash shell.

Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

3 thoughts on “How to check if two .pyc files are the same byte code

  1. Nice. I have something in a pyc and no idea if its the older version or not(of course its my code but still confusing). This may help telling the two apart.

  2. I had two versions of the same module, but both were compiled.
    They were different in size (probably because I transfered them between windows and unix and new lines were turned to \r\n in the process or because they were compiled by different versions of python.
    Anyway, I compared between them with this:

    import version1 as v1
    import version2 as v2

    dico = ico.__dict__
    dbico = bico.__dict__
    print set(dico.keys()).difference(dbico.keys())
    print set(dbico.keys()).difference(dico.keys())

    import types
    for k,v in dico.iteritems():
    if type(v)!=type(dbico[k]):
    print "types differ",k
    elif type(v) == types.FunctionType:
    print "function: ",k
    if v.__code__!=dbico[k].__code__:
    print "codes differ",k

Leave a Reply to chris frost Cancel reply

Your email address will not be published. Required fields are marked *