0

I'm using python installed in a loc My file structure is set up as following

adsb_comm
     msg
          adsb.msg
          flarm.msg
          flighttracking.msg
     src
          __init__.py
          main.py
          test.py
     __init__.py
     CMakeLists.txt
     package.xml

I try to import the messages for my code follows

#!/usr/bin/env python
import rospy
from adsb_comm.msg import adsb, flarm, flightTracking

When writing code, VSC recognizes this perfectly but when I try to run the code, it always results in the following error:

Traceback (most recent call last):
  File "/home/joren/catkin_ws/src/adsb_comm/src/main.py", line 5, in <module>
    from Parser import adsbMod, flarmMod, statMod
  File "/home/joren/catkin_ws/src/adsb_comm/src/test.py", line 3, in <module>
    from adsb_comm.msg import adsb, flarm, flightTracking
ImportError: No module named adsb_comm.msg

When I ran

>>> import sys
>>> sys.path

I got

['', '/opt/ros/melodic/lib/python2.7/dist-packages', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/joren/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk3']

But I am not certain what to make of this. I also tried using a relative import instead, along the lines of

from ..msg import adsb

but got a value error

ValueError: Attempted relative import in non-package

What could I be missing here?

Joren V
  • 41
  • 5
  • have you tried just `from adsb_comm import adsb, flarm, flightTracking` removing the extension of the file '.msg' ? – Benoit Drogou Aug 03 '21 at 13:44
  • 1
    I don't understand why you expect to be able to import Python code from a file with a `.msg` extension. What does that extension even mean? – Karl Knechtel Aug 03 '21 at 13:45
  • @BenoitDrogou in the code, the `.msg` refers to the `msg` subdirectory of `adsb_comm`, not the file extension. – Karl Knechtel Aug 03 '21 at 13:46
  • Does https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time answer your question? – Karl Knechtel Aug 03 '21 at 13:47
  • that first '.msg' is just the folder in which the msg files are located. Removing it gives me `ImportError: No module named adsb_comm` – Joren V Aug 03 '21 at 13:47
  • @KarlKnechtel the .msg files are required for ROS. [link](http://wiki.ros.org/ROS/Tutorials/CustomMessagePublisherSubscriber%28python%29) if you're interested. The problem wasn't with the extension but with the fact that ROS had not been properly sourced. – Joren V Aug 03 '21 at 14:19

0 Answers0