Michael576
Posts: 1
Joined: Sun Oct 13, 2019 2:49 pm

I try to run the mypi_server.pyc and get the RuntimeError: Bad magic number in .pyc file

Sun Oct 13, 2019 2:56 pm

Hi

I would like to operate equipment via internet. I am trying to use MyPi. I have followed instructions on many websites but always get the same error.

I try to run the mypi_server.pyc and get the RuntimeError: Bad magic number in .pyc file

Pulling my hair out!!!!

hippy
Posts: 12527
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: I try to run the mypi_server.pyc and get the RuntimeError: Bad magic number in .pyc file

Mon Oct 14, 2019 11:18 am

I would expect the error indicates the Python version you are using to run the .pyc file is not the same as the version which created the .pyc file.

This is why it's not recommended to distribute .pyc files as applications.

Using the "file mypi_server.pyc" should hopefully indicate which version of Python the .pyc file was built with.
Last edited by hippy on Mon Oct 14, 2019 11:22 am, edited 1 time in total.

User avatar
DougieLawson
Posts: 42232
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: I try to run the mypi_server.pyc and get the RuntimeError: Bad magic number in .pyc file

Mon Oct 14, 2019 11:22 am

You should delete the *.pyc file and run the original program that imports mypi_server.py.

A pyc is a python class file and is created (automagically) when a module is imported. You can't run those things directly.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

hippy
Posts: 12527
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: I try to run the mypi_server.pyc and get the RuntimeError: Bad magic number in .pyc file

Mon Oct 14, 2019 11:28 am

DougieLawson wrote:
Mon Oct 14, 2019 11:22 am
A pyc is a python class file and is created (automagically) when a module is imported. You can't run those things directly.
Yes one can, providing the same Python version is used to run the .pyc as the Python version which created it ...

Code: Select all

pi@Pi3B:~/tmp $ cat test.py
import xyzzy
pi@Pi3B:~/tmp $ cat xyzzy.py
print("In xyzzy.py")
pi@Pi3B:~/tmp $ python2 test.py
In xyzzy.py
pi@Pi3B:~/tmp $ python2 xyzzy.pyc
In xyzzy.py
pi@Pi3B:~/tmp $ python3 xyzzy.pyc
RuntimeError: Bad magic number in .pyc file
pi@Pi3B:~/tmp $ 

hippy
Posts: 12527
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: I try to run the mypi_server.pyc and get the RuntimeError: Bad magic number in .pyc file

Mon Oct 14, 2019 11:46 am

Michael576 wrote:
Sun Oct 13, 2019 2:56 pm
I try to run the mypi_server.pyc and get the RuntimeError: Bad magic number in .pyc file
You are probably using the "python" command rather than "python3" -

http://www.ioslinks.com/mypi/help.html

Code: Select all

pi@Pi3B:~/tmp $ wget www.ioslinks.com/mypi/server.zip
 ... snip ...
pi@Pi3B:~/tmp $ unzip server.zip
Archive:  server.zip
  inflating: mypi_server.pyc         
  inflating: mypi.cfg                
pi@Pi3B:~/tmp $ python mypi_server.pyc 
RuntimeError: Bad magic number in .pyc file
pi@Pi3B:~/tmp $ python3 mypi_server.pyc 
MyPi TCP Server v1.6
Loading configuration file: /home/pi/tmp/mypi.cfg
... snip ...

User avatar
scruss
Posts: 5015
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON

Re: I try to run the mypi_server.pyc and get the RuntimeError: Bad magic number in .pyc file

Mon Oct 14, 2019 12:00 pm

Or you can run the pyc through uncompyle6 and get the source back. Then it should run in any Python 3 inter

Looking at the source, MyPi seems to collect your name, email and IP address if you use their dynamic DNS server. Might be a GDPR stumble if you're in Europe.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

hippy
Posts: 12527
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: I try to run the mypi_server.pyc and get the RuntimeError: Bad magic number in .pyc file

Mon Oct 14, 2019 12:21 pm

scruss wrote:
Mon Oct 14, 2019 12:00 pm
Or you can run the pyc through uncompyle6 and get the source back. Then it should run in any Python 3 inter
Funnily enough I was just trying that myself.

It is utterly pointless to try and hide pure Python source code by distributing a .pyc file when it can be so easily decompiled.

Code: Select all

cd ~
git clone https://github.com/rocky/python-uncompyle6.git
cd python-uncompyle6
pip install -e .
sudo python setup.py install

Code: Select all

uncompyle6 -o mypi_server.py mypi_server.pyc

Return to “Troubleshooting”