USC-ACTLab / crazyswarm

A Large Quadcopter Swarm
MIT License
328 stars 324 forks source link

ImportError for ROS generated modules in Python 2 #455

Closed santiagorg2401 closed 3 years ago

santiagorg2401 commented 3 years ago

Hi, I hope you're all doing well, I am having trouble with the latest update, I re-installed everything related to crazyswarm on my PC but now when I try to run a script (even with the provided ones) I get:

Traceback (most recent call last):
  File "niceHover.py", line 9, in <module>
    swarm = Crazyswarm()
  File "/home/santiagorg2401/crazyflie/crazyswarm/ros_ws/src/crazyswarm/scripts/pycrazyswarm/crazyswarm.py", line 50, in __init__
    from .crazyflie import TimeHelper, CrazyflieServer
  File "/home/santiagorg2401/crazyflie/crazyswarm/ros_ws/src/crazyswarm/scripts/pycrazyswarm/crazyflie.py", line 12, in <module>
    from crazyswarm.srv import *
ImportError: No module named srv

I already tried re-installing again several times but that didn't work.

Thank you for your time.

whoenig commented 3 years ago

I just tested it and it seems to work. Did you forget to call source devel/setup.bash in the terminal that you use to launch the script from?

santiagorg2401 commented 3 years ago

Hi, I sourced in the terminals when I got the error, it only happens in non-simulations execution, in other words, when running with --sim it doesn't happen.

The-SS commented 3 years ago

I had this issue on Ubunut 16.04 with Python 2. It might be related to this, tl;dr, python confuses the file name and the ros package name. I solved it by renaming pycrazyswarm/crazyswarm.py to pycrazyswarm/crazyswarm_py.py , and editing pycrazyswarm/__init__.py: instead of from .crazyswarm import * use from .crazyswarm_py import *.

whoenig commented 3 years ago

I did test with real flight. The --sim option actually doesn't use ROS at all and therefore doesn't try to import the service description. I tested on Ubuntu 20.04, ROS Noetic, Python 3.

santiagorg2401 commented 3 years ago

I forgot to tell you my Ubuntu version (my bad), I have Ubuntu 18.04, ROS Melodic & Python 2.

The-SS commented 3 years ago

Yes, this seems to be a Python 2 issue.

santiagorg2401 commented 3 years ago

Well, I installed Ubuntu 20 but now I have trouble installing, however, I'll open another issue for that.

cscholler commented 3 years ago

I had this issue on Ubunut 16.04 with Python 2. It might be related to this, tl;dr, python confuses the file name and the ros package name. I solved it by renaming pycrazyswarm/crazyswarm.py to pycrazyswarm/crazyswarm_py.py , and editing pycrazyswarm/__init__.py: instead of from .crazyswarm import * use from .crazyswarm_py import *.

Using Melodic on Ubuntu 18.04 with python 2 I was having this same "No module named srv" import error. Renaming these files and removing pycrazyswarm.pyp fixed it for me.

jpreiss commented 3 years ago

I saw this today too. I am guessing this bug was introduced by 9037a25c8390fbc68f1dc335f8131545d6cb6012. We should rename the file, I think.

It would be nice if we could catch bugs like this in CI. In descending order of usefulness (and effort), we could...

  1. finish #350, then run all current unit tests with the ROS sim
  2. make sure the server and pycrazyswarm still run when crazyflies.yaml contains 0 entries; write a test that starts the Crazyswarm server and verifies that a pycrazyswarm script can find the services and spin ROS once
  3. write a test to make sure we can import pycrazyswarm in ROS, but never actually construct the Crazyswarm object
jpreiss commented 3 years ago

failing test for option 3 (see previous comment) added in https://github.com/USC-ACTLab/crazyswarm/tree/rosCI_rebase

jpreiss commented 3 years ago

Fixed and tested on u16 and u20.