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

workspace global python module - where to put?

asked 2013-08-02 05:44:31 -0600

Hendrik Wiese gravatar image

I'd like to have a python module globally available across all nodes in a catkin workspace. It contains functions that I don't want to repeat in every module I need them. They are pure Python modules. I'd like to do something like this:

#!/usr/bin/env python

def myFunction(a):
  # just an example
  something = a * 2
  return something

# End of

And in my ROS Python scripts I'd like to be able to import that module like...

#!/usr/bin/env python
import roslib...
import mymodule

def someotherfunc():
  b = mymodule.myFunction(10)

if __name__ ==...

It shall be available in my whole catkin workspace. Where do I have to put it?

Thanks a lot!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-08-02 06:20:08 -0600

updated 2013-08-02 06:32:11 -0600

I think this tutorial covers what you would like to do:


1. Create in your package root directory with these contents (where <python_mod> is replaced by the name of your python module, typically the same name as your catkin package)


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

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


2. Add the following line to your CMakeLists.txt


3. Create a directory for your python scripts (if you have any python executables)

mkdir <pkg_root>/scripts

4. Create a directory for your python modules

mkdir <pkg_root>/src/<python_mod>/

5. Add an empty to your python module directory

touch <pkg_root>/src/<python_mod>/

6. create your python mod

touch <pkg_root>/src/<python_mod>/

You should now be able to import this module as

import python_mod.mod1

Hope that helps!

Reference the tutorial for details.

edit flag offensive delete link more


Yeah, works like a charm! Thanks a lot! :-)

Hendrik Wiese gravatar image Hendrik Wiese  ( 2013-08-06 03:45:11 -0600 )edit

Question Tools

1 follower


Asked: 2013-08-02 05:44:31 -0600

Seen: 192 times

Last updated: Aug 02 '13