Why does my python script break after compiling?
<p>I'm checking out how much of a performance increase I get after compiling a python script. After research looking into this issue I don't think I will actually see an increase in performance with the script I have written because I found out that once the script is loaded, the execution time doesn't increase. I still would like to know why this is failing to run after compiling as this is my first time trying this. Here is my script</p>

<pre><code>#!/bin/python3
from datetime import datetime
start = datetime.now()

import psutil

BYTES_PER_GB = 1024*1024*1024

# Memory
m = psutil.virtual_memory()
#total = m.total/BYTES_PER_GB 
#available = m.available/BYTES_PER_GB
#used = m.used/BYTES_PER_GB
m_free= m.free/BYTES_PER_GB
m_percent = m.percent

# Swap
s = psutil.swap_memory()
s_free = s.free/BYTES_PER_GB
s_percent = s.percent

print('ï %.1fG (%.1f%%) ï %.1fG (%.1f%%)' % (m_free, m_percent, s_free, s_percent))
print('ï %.1fG (%.1f%%) ï %.1fG (%.1f%%)' % (m_free, m_percent, s_free, s_percent))

print(datetime.now() - start)
</code></pre>

<p>I'm trying to compile with this line</p>

<pre><code>python3 -m py_compile memory
</code></pre>

<p>In my print statements I have some special characters from font awesome. Not sure if that would cause a problem but if it doesn't show up correctly in my post then that's what that is.</p>

<p>The output when I try to run the compiled file is </p>

<pre><code>./memorycpython-35.pyc: line 1: $'\026\r\r': command not found
./memorycpython-35.pyc: line 2: ï¿½kï¿½Wï¿½ï¿½@sï¿½ddlmZejï¿½ZddlZdZejï¿½Zejeej: command not found
./memorycpython-35.pyc: line 3: syntax error near unexpected token `)'
./memorycpython-35.pyc: line 3: `ej
                                ï¿½Z
                                  e
e                                      je
 j  Zeï¿½eejï¿½eï¿½dS)ï¿½)datetimeNiii@)rZnowï¿½startZpsutilZ
                                                                   BYTES_PER_GBZvirtual_memoryï¿½mZfreeZm_freeZpercentZ   m_percentZ
              swap_memoryï¿½sZs_freeZ s_percentï¿½printï¿½rrï¿½memory&lt;module&gt;s



       '
   ^[[?62;c^[[?62;c
</code></pre>

<p><strong><em>EDIT</em></strong></p>

<p>To narrow down the problem I wrote the following script</p>

<pre><code>#!/bin/python3
print("Hello World!")
</code></pre>

<p>This is the output</p>

<pre><code>./testcpython-35.pyc: line 1: $'\026\r\r': command not found
./testcpython-35.pyc: line 2: syntax error near unexpected token `)'
./testcpython-35.pyc: line 2: `ï¿½rï¿½W%ï¿½@sedï¿½dS)z
                                               Hello World!N)ï¿½printï¿½rrï¿½./test&lt;module&gt;s'
</code></pre>

<p>Compiled using</p>

<pre><code>python3 -m py_compile ./test
</code></pre>

<p>This creates a file in <code>__pycache__/</code> called <code>testcpython-35.pyc</code> which I then do <code>chmod +x testcpython-35.pyc</code> and <code>./testcpython-35.pyc</code></p>

<p>It appears my issue is the <code>./testcpython-35.pyc</code> part. When I run <code>python3 testcpython-35.pyc</code>, independent on whether I did <code>chmod +x ./testcpython-35.pyc</code>, the output is scrabbled. As long as I run the compiled program by first specifying what program to run it with, <code>python3</code>, it outputs <code>Hello World!</code> as expected.</p>

python python-3.x