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

How to avoid hardcoding package name in python?

asked 2011-12-08 12:40:21 -0500

130s gravatar image

updated 2011-12-14 04:55:42 -0500

#!/usr/bin/env python
PACKAGE_NAME = 'puddingPackage'
import roslib; roslib.load_manifest(PACKAGE_NAME)
  :
  :

Other than declaring in each .py files like above, what is the official/recommended way to give the package name to roslib that's mandatory in rospy?

Might / Should not be often the case but in my team the pkg name gets changed esp. at its earlier stage where the usage and stack/pkg structure isn't mature enough. Thank you.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2011-12-11 21:15:59 -0500

felix k gravatar image

updated 2011-12-18 20:57:35 -0500

Workaround?

#!/usr/bin/env python
import os, sys

#pkg_name = os.path.basename(os.getcwd()) # not working when called from wherever
#pkg_name = os.path.basename(os.path.realpath(sys.path[0])) # might work in every case
pkg_name = os.path.basename(os.path.dirname(os.path.realpath(__file__))) # should work in every case

if pkg_name in ['src','bin','python']: # typical sub dirs 
    pkg_name = os.path.basename(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
import roslib; roslib.load_manifest(pkg_name)
  :

A "more proper but expensive" solution would be to recursively search the path for a manifest.xml and parse that.

edit flag offensive delete link more

Comments

What happens in that case if you rosrun a python script from another package?
dornhege gravatar image dornhege  ( 2011-12-12 00:15:27 -0500 )edit
Oh, yeah, that's the old cwd thing. Did some research and made an edit. Seems to be quite competent, unless you import another module instead of running it, at which you might have to use 'inspect'...
felix k gravatar image felix k  ( 2011-12-13 01:15:18 -0500 )edit
roslib actually has something built in to do this: pkg_dir, pkg_name = roslib.packages.get_dir_pkg(os.getcwd()). That said, I'm sure there's a reason this isn't standard practice.
Dan Lazewatsky gravatar image Dan Lazewatsky  ( 2011-12-13 02:34:39 -0500 )edit
Thx guys, the one @felix k suggests works out for me for now. Also nice to know ROS function like get_dir_pkg. So far it sheds error in my env. (looks like related to roslib/packages.py#get_pkg_dir()) as it mentions itself as "not 100% stable", but I'll keep it mind too.
130s gravatar image 130s  ( 2011-12-13 10:24:19 -0500 )edit
@IsaacSaito: I just updated the if-inside path call, as I forgot to change it to __file__.
felix k gravatar image felix k  ( 2011-12-18 21:04:24 -0500 )edit
1

answered 2011-12-09 00:56:45 -0500

joq gravatar image

updated 2011-12-09 00:59:04 -0500

What you show is the usual practice. I can't think of a better way.

This is just one of the many reasons why changing package names is painful. I recommend giving package names more thought early in the project.

edit flag offensive delete link more

Comments

1
@joq thank you. It's just a little surprising that ROS isn't equipped with functionality to allow such a simple flexibility like this case. Btw your "more thought" should be shared somewhere top level in ROS' wiki IMHO.
130s gravatar image 130s  ( 2011-12-09 03:48:34 -0500 )edit

Question Tools

Stats

Asked: 2011-12-08 12:40:21 -0500

Seen: 1,574 times

Last updated: Dec 18 '11