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

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
# mymodule.py

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

# End of mymodule.py

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
1

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:

summary:

1. Create setup.py 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)

## ! 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=['<python_mod>'],
    package_dir={'': 'src'},
    requires=['std_msgs', 'rospy'])
)

setup(**setup_args)

2. Add the following line to your CMakeLists.txt

catkin_python_setup()

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 __init__.py to your python module directory

touch <pkg_root>/src/<python_mod>/__init__.py

6. create your python mod

touch <pkg_root>/src/<python_mod>/mod1.py

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

Comments

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

Stats

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

Seen: 254 times

Last updated: Aug 02 '13