title: Reassemble .py file from bytecode
body: <p><strong>Problem Statement</strong></p>
  
  <p>I have a file (no extension) with some nicely formatted python opcodes that I would like to reassemble into the original .py file (or as close as I can).</p>
  
  <p><strong>Recreating Problem</strong></p>
  
  <p>I can recreate a file like the one I have. Begin with a file called <code>test.py</code>, with the contents:</p>
  
  <pre><code>a = 1
  b = 2
  print(a+b)
  </code></pre>
  
  <p>By running <code>python3 -m dis test.py</code>, I get the following output:</p>
  
  <pre><code>  1       0 LOAD_CONST               0 (1)
            2 STORE_NAME               0 (a)
  
    2       4 LOAD_CONST               1 (2)
            6 STORE_NAME               1 (b)
  
    3       8 LOAD_NAME                2 (print)
           10 LOAD_NAME                0 (a)
           12 LOAD_NAME                1 (b)
           14 BINARY_ADD
           16 CALL_FUNCTION            1
           18 POP_TOP
           20 LOAD_CONST               2 (None)
           22 RETURN_VALUE
  </code></pre>
  
  <p>I would like to reconstruct the original <code>test.py</code> file from this output.</p>
  
  <p><strong>What I've tried</strong></p>
  
  <p>I have already tried running <code>uncompyle6</code> on the output, but it errors out with the following message:</p>
  
  <pre><code>ImportError: Unknown magic number 8224 in test.pyc
  </code></pre>
  
  <p>I do not know the original python version used to generate the original file to obtain the magic number, nor do I know if the magic number is the only thing missing from the file.</p>
  
  <p>Someone has asked a similar question here a long time ago: <a href="https://stackoverflow.com/questions/2124202/reassembling-python-bytecode-to-the-original-code">Reassembling Python bytecode to the original code?</a> The proposed answer is antiquated, but even following the updates, the current answer should be to use <code>uncompyle6</code>, but I can't seem to get that to work.</p>