Organize and Reference Ros Code in Python Packages

asked 2017-02-11 03:09:38 -0500

thomascheney gravatar image

I apologize if this is duplicate, but I've been searching google and this site for hours and haven't been able to figure this out yet. I simply would like to organize my code into Python packages and be able to reference one package from another.

My setup looks like this. I have a top level ROS package called aimbot. Under that I have a python package called game and another one called vision (under one other package). I'd like to be able to import a class called Position from Position.py found in the game package into a script I have in the vision class. I'm was trying to import with a line that looks like this: from ...game.Position import Position (which PyCharm thinks is valid). Initially I was getting this error: ValueError: Attempted relative import in non-package. However, after reading around a bit, I found something that mentioned that you can't do relative imports, so I changed it to: aimbot.game.Position, which got me a bit further. However, now I get the error "ImportError: No module named game.Position" and none of the many things I've tried to fix it have worked. After reading around a bit, I gathered I needed to add a setup.py to my aimbot folder, so created one that looked like this:

## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

# fetch values from package.xml
setup_args = generate_distutils_setup(
    packages=['aimbot'],
    package_dir={'': 'src'},
)

setup(**setup_args)

I tried several other variations of this including with 'game' and the path to game for the packages option, but nothing I have tried has changed anything. I gather that I need to let ROS know about the game package somehow, but I haven't been able to guess the correct solution, and I can't find the right resource either and/or just haven't understood it if I have. How can I fix this error? Thanks for the help.

edit retag flag offensive close merge delete

Comments

FYI: there is a difference between a Catkin python package and plain Python packages. Not in the code, but mostly how they are distributed / interact with other Catkin packages.

And just making sure: you've seen http://docs.ros.org/kinetic/api/catki...

gvdhoorn gravatar image gvdhoorn  ( 2017-02-11 04:50:28 -0500 )edit

Not sure if this is right but I was thinking I have just one catkin package (aimbot) and the rest are python packages . Do I have to somehow set them all up as python packages?

thomascheney gravatar image thomascheney  ( 2017-02-11 11:30:19 -0500 )edit