Reproducible builds in python
<p>I need to ship a compiled version of a python script and be able to prove (using a hash) that the compiled file is indeed the same as the original one.</p>

<p>What we use so far is a simple:</p>

<pre><code>find . -name "*.py" -print0 | xargs -0 python2 -m py_compile
</code></pre>

<p>The issue is that this is not reproducible (not sure what are the fluctuating factors but 2 executions will not give us the same .pyc for the same python file) and forces us to always ship the same compiled version instead of being able to just give the build script to anyone to produce a new compiled version.</p>

<p>Is there a way to achieve that?</p>

<p>Thanks</p>

<p>Compiled Python files include a four-byte magic number and the four-byte datetime of compilation. This probably accounts for the discrepancies you are seeing.</p>

<p>If you omit bytes 5-8 from the checksumming process then you should see constant checksums for a given version of Python.</p>

<p>The format of the <code>.pyc</code> file is given in <a href="http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html">this blog post</a> by Ned Batchelder.</p>

python binary-reproducibility