ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Exporting Python Module With Same Name as Package, with srv/msg

asked 2014-08-14 10:50:04 -0500

tfurf gravatar image

updated 2014-08-18 06:29:18 -0500

EDIT: MWE can be found here.

Per [1] and [2], I'm trying to export a python module that is the same name as the package:

packagename
 CMakeLists.txt
 package.xml               # catkin_python_setup()
 setup.py                  # generate_distutils_setup( ..., packages=['packagename'], ... )
 |- src/
    |- application.py         # import packagename
    |- packagename/
      |- __init__.py
      |- stuff.py
      |- ...

This works as expected, no problem. However, if I introduce a service message and try to import it:

packagename
 CMakeLists.txt
 package.xml
 setup.py
 |- srv/
    |- MySrv.srv
 |- src/
    |- application.py         # import packagename
    |- packagename/
      |- __init__.py
      |- stuff.py
      |- use_my_service.py  # from packagename.srv import MySrv
      |- ...

At runtime I get

ImportError: No module named srv

It works if I change the name of the exported module:

packagename
 CMakeLists.txt
 package.xml
 setup.py                  # generate_distutils_setup( ..., packages=['notpackagename'], ... )
 |- srv/
    |- MySrv.srv
 |- src/
    |- application.py         # import notpackagename
    |- notpackagename/
      |- __init__.py
      |- stuff.py
      |- use_my_service.py # from notpackagename.srv import MySrv
      |- ...

The path in both cases is exactly the same, including

$WORKSPACE_ROOT/devel/lib/python2.7/dist-packages

And in both cases I can locate the appropriate pacakgename/srv/_MySrv.py. In the working case, the output directory structure looks like:

devel/lib/python2.7/dist-packages/
|- notpackagename
  |- __init__.py     # Non-empty, dist-utils generated
|- packagename
  |- __init__.py     # Empty.
  |- srv/
    |- __init__.py 
    |- _MySrv.py

And in the non-working case:

devel/lib/python2.7/dist-packages/
|- packagename
  |- __init__.py     # Non-empty, dist-utils generated.
  |- srv/
    |- __init__.py 
    |- _MySrv.py

It appears that the generated __init__.py for the exported module clobbers the knowledge of the existence of submodules, but I'm not sure, I'm not particularly familiar with how catkin does it's magic for this.

Is my expectation for this to work misguided? If it's not supposed to/going to work, what is the preferred method? To prefix the module with lib, as in [3] ?

edit retag flag offensive close merge delete

Comments

What exact command are you invoking when you get the ImportError? Do you invoke src/application.py as a script / node?

Dirk Thomas gravatar image Dirk Thomas  ( 2014-08-14 12:49:43 -0500 )edit

What exact command are you invoking when you get the ImportError?

The exception is raised right at import-time, in use_my_service.py (part of the module). The specific line looks like from packagename.srv import MySrv.

tfurf gravatar image tfurf  ( 2014-08-18 03:41:55 -0500 )edit

Do you invoke src/application.py as a script / node?

The src/application.py is called as a node, via roslaunch, with a simple .launch file.

tfurf gravatar image tfurf  ( 2014-08-18 03:42:50 -0500 )edit

See https://github.com/tfurf/rospy-srv-mo... for both working and non-working versions.

tfurf gravatar image tfurf  ( 2014-08-18 06:30:08 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-08-18 10:35:12 -0500

Dirk Thomas gravatar image

Placing your node under src/application.py will interfere with the package resolution of Python. When you import anything from packagename it will resolve the relative subfolder rather then the folder in devel space which actually provides your generated message files.

If you stick to the recommendation from the tutorials to place your node scripts in either a nodes or scripts folder (instead of into src) the problem should disappear.

edit flag offensive delete link more

Comments

Thanks for the hint. This wasn't clear to me after reading http://wiki.ros.org/PyStyleGuide#Pack... .

tfurf gravatar image tfurf  ( 2014-08-19 03:09:11 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-08-14 10:50:04 -0500

Seen: 3,065 times

Last updated: Aug 18 '14