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
#!/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)
I'm trying to compile with this line
python3 -m py_compile memory
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.
The output when I try to run the compiled file is
./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<module>s
'
^[[?62;c^[[?62;c
EDIT
To narrow down the problem I wrote the following script
#!/bin/python3
print("Hello World!")
This is the output
./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<module>s'
Compiled using
python3 -m py_compile ./test
This creates a file in __pycache__/
called testcpython-35.pyc
which I then do chmod +x testcpython-35.pyc
and ./testcpython-35.pyc
It appears my issue is the ./testcpython-35.pyc
part. When I run python3 testcpython-35.pyc
, independent on whether I did chmod +x ./testcpython-35.pyc
, the output is scrabbled. As long as I run the compiled program by first specifying what program to run it with, python3
, it outputs Hello World!
as expected.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments