junzis / pyModeS

Python decoder for Mode S and ADS-B signals
GNU General Public License v3.0
527 stars 151 forks source link

Cannot import c_common #77

Closed jmceara closed 3 years ago

jmceara commented 3 years ago

Hi! I was using an old version of pyModeS to make some tests. Recently I've updated to latest version, installation is ok, but I can't run anything, even modeslive. It's a bug in pyModeS or am I missing something? I've setup python3 as default for my system.

pi@rasp3_dev:~ $ sudo pip3 install git+https://github.com/junzis/pyModeS
Collecting git+https://github.com/junzis/pyModeS
  Cloning https://github.com/junzis/pyModeS to /tmp/pip-cm6ej86e-build
  Requirement already satisfied (use --upgrade to upgrade): pyModeS==2.8 from git+https://github.com/junzis/pyModeS in /usr/local/lib/python3.5/dist-packages
Requirement already satisfied: numpy in /usr/local/lib/python3.5/dist-packages (from pyModeS==2.8)
Requirement already satisfied: pyrtlsdr in /usr/local/lib/python3.5/dist-packages (from pyModeS==2.8)
Requirement already satisfied: pyzmq in /usr/local/lib/python3.5/dist-packages (from pyModeS==2.8)
pi@rasp3_dev:~ $ sudo pip install pyModeS
Requirement already satisfied: pyModeS in /usr/local/lib/python3.5/dist-packages
Requirement already satisfied: numpy in /usr/local/lib/python3.5/dist-packages (from pyModeS)
Requirement already satisfied: pyrtlsdr in /usr/local/lib/python3.5/dist-packages (from pyModeS)
Requirement already satisfied: pyzmq in /usr/local/lib/python3.5/dist-packages (from pyModeS)
pi@rasp3_dev:~ $ modeslive --help
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.5/site-packages/pyModeS/__init__.py", line 5, in <module>
    from . import c_common as common
ImportError: cannot import name 'c_common'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/modeslive", line 10, in <module>
    from pyModeS.streamer.decode import Decode
  File "/home/pi/.local/lib/python3.5/site-packages/pyModeS/__init__.py", line 8, in <module>
    from . import py_common as common
  File "/home/pi/.local/lib/python3.5/site-packages/pyModeS/py_common.py", line 130
    addr: Optional[str]
        ^
SyntaxError: invalid syntax
pi@rasp3_dev:~/pyModeS $ python -V
Python 3.5.3

also, I'm trying to use this script, but got the same import error:

#!/usr/bin/python3 -O

import pyModeS as pms
import sys

for msg in sys.stdin:
    msg = msg.replace("*", "").replace(";", "")
    #if len(msg) > 20 and msg[0:4] == "A000" and pms.bds.bds44.is44(msg) and not pms.bds.bds40.is40(msg) :
    if len(msg) > 20 and msg[0:4] == "A000" and pms.bds.bds44.is44(msg, rev=False)  :
        #print hex(pms.bin2int(pms.crc(msg, encode=False)))
        print ("*" + msg.replace("\n","").lower() + ";")
        #sys.stdout.write('Wind speed/dir: ')
        print (pms.commb.wind44(msg, rev=False))  # Wind speed (kt) and direction (true) (deg)
        #sys.stdout.write('Temp: ')
        print (pms.commb.temp44(msg, rev=False))  # Static air temperature (C)
        #sys.stdout.write('hPa: ')
        print (pms.commb.p44(msg, rev=False))     # Average static pressure (hPa)
        #sys.stdout.write('Hum: ')
        print (pms.commb.hum44(msg, rev=False))   # Humidity (%)
        print
jmceara commented 3 years ago

Ok, I've removed packages (pyModeS*) from .local dir (in home from user) and from /usr/local/lib/python3.5/dist-packages/. Done install again.....still same problem, with different path:

pi@rasp3_dev:~/pyModeS $ modeslive --help
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/pyModeS-2.8-py3.5.egg/pyModeS/__init__.py", line 5, in <module>
    from . import c_common as common
ImportError: cannot import name 'c_common'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/modeslive", line 4, in <module>
    __import__('pkg_resources').run_script('pyModeS==2.8', 'modeslive')
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 739, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1494, in run_script
    exec(code, namespace, namespace)
  File "/usr/local/lib/python3.5/dist-packages/pyModeS-2.8-py3.5.egg/EGG-INFO/scripts/modeslive", line 10, in <module>
    from pyModeS.streamer.decode import Decode
  File "/usr/local/lib/python3.5/dist-packages/pyModeS-2.8-py3.5.egg/pyModeS/__init__.py", line 8, in <module>
    from . import py_common as common
  File "/usr/local/lib/python3.5/dist-packages/pyModeS-2.8-py3.5.egg/pyModeS/py_common.py", line 130
    addr: Optional[str]
        ^
SyntaxError: invalid syntax
imanolpg commented 3 years ago

Hello @jmceara.

Try removing and installing all again with pip: pip3 remove pyModeS and pip3 install pyModeS.

If the error is still there, make sure that in the module directory /usr/local/lib/python3.5/dist-packages/pyModeS exists the files _ccommon.pxd and _ccommon.pyx

junzis commented 3 years ago

@jmceara, @imanolpg, (and @xoolive FYI)

I did some tests with different Python versions. The error is caused by variable annotation (PEP 526) in the py_common module. It only effects Python version 3.5 and below.

So, the simplest option is to update you Python to 3.6+ for now.

I will evaluate the code to see if we should keep the variable annotation, or make Python 3.6 the minimum required version in the setup.

xoolive commented 3 years ago

Indeed variable annotation syntax is only valid from Python 3.6 (to put things in perspective, Python 3.9rc1 is just out!)

From Python 3.6 we can write

addr: Optional[str] = None

In Python 3.5, the following syntax is valid and should be readable by MyPy (second part to be confirmed...)

addr = None  # type: Optional[str]

The first syntax is more flexible but the second should do the job if we just want type hints as information. So yes, in the end @junzis, the only question is when do you want to stop support for Python 3.5

jmceara commented 3 years ago

ok, got it working with version 3.6.12 of python....but I have another problem. Now I'm getting this error:


File "./jonis.py", line 9, in <module>
    if len(msg) > 20 and msg[0:4] == "A000" and pms.bds.bds44.is44(msg)  :
AttributeError: module 'pyModeS' has no attribute 'bds'```

But this code is the smae as I was running before.....and also, this is the same code (isbds44) in the main README file. Maybe doc is outdated?
What's the correct way to check now?
junzis commented 3 years ago

This seems strange. But I can't reproduce this.

Please provide a minimum working example, and the pyModeS version you are using.

jmceara commented 3 years ago

It's exaclty the same code from my first post in this issue, except for the python path that I've changed from #!/usr/bin/python3 -O to #!/usr/local/bin/python3.6 -O

I'm using python 3.6.12. program modeslive run fine, without any problem.

Note: Except for the import error in first msg of this topic, the code ran fine in previous version of python (and pyModeS). I mean, before updating pyModeS and python, I was using exactly the same code. I don't remember exactly which version of pyModeS, but it was before BDS4,5 was implemented, if I'm right.....am not sure.

junzis commented 3 years ago

I think it is a problem with installation.

Maybe try to remove pyModeS use pip uninstall, and then install it again to see if it solves the issue.

If it still doesn't work, try to install and run it the script in a python virtual environment.

If you are struggling with native python library on Linux, the easiest way is to use a python package management tool like anaconda.