[Documentation] [TitleIndex] [WordIndex

PyStyleGuide

This page defines a style guide to be followed in writing Python code for ROS. This guide applies to all ROS code, both core and non-core.

For C++, see the C++ Style Guide and for Javascript, see the ROS JavaScript Style Guide

Coding Style

Python code should follow PEP 8. PEP 8 is not a strict style guide and values readability over consistency, so just try to be smart. A quick summary of PEP 8 is:

The Loader

This paragraph is only necessary for packages which use rosbuild/rosmake. If the package is using catkin it must not use roslib.load_manifest.

roslib is the only ROS Python package you can automatically assume to be importable other than your local files. If your Python package has dependencies, you must include the following header at the top:

import roslib
roslib.load_manifest('your_package_name')

roslib.load_manifest looks at your Manifest and places your dependencies on your Python path. DO NOT use multiple load_manifest calls. If you need multiple calls, it's probably because you're missing the correct dependencies in your manifest.

Package/Module Names (__init__.py files)

All python code must be placed within a module namespace. ROS exports your Python source directory to be on the path of any of your dependencies, so it is important not to accidentally clobber someone else's import. We strongly recommend that this module name be the same as your ROS package name.

There are two recommended code layouts:

Small modules with no msg/srvs:

packagename
 |- src/
    |- packagename.py
 |- scripts/
    |- non-exported python files

Module with msgs/srvs

packagename
 |- src/
    |- packagename/
      |- __init__.py
      |- yourfiles.py
 |- scripts/
    |- non-exported python files

If you don't know what an __init__.py file is, we recommend that you read What is init py used for?

The more complicated layout for msg/srv files is required as the Python msg/srv generators will need to generate files into your package's namespace.

In the rare case that you can't place your source code in src/ (e.g. thirdparty code), you can override the Python export path of your package by editing your manifest.

See the next section for description of node files

Node Files

In ROS, the name of a node type is the same as its executable name. Typically, for python files, this means including #!/usr/bin/env python at the top of your main script, and having the main script's name == node's name.

If your node is simple, this script may contain the entire code for it. Otherwise, the node file will likely do an import packagename and invoke code there.

NOTE: we strive to keep ROS-specific code separate from reusable, generic code. The separation of 'node files' and files you place in src/packagename helps encourage this.

Python Features/Version

Our target is Python 2.5, though we wish to make code that is easily transitioned to Python 2.6, 2.7, Python3k, etc... (see PEP 3100). This means:

All the above are on this list either because:

If you find one of your favorites on this list marked for death, please blame Guido.

NOTE: the ROS python code base is still being transitioned to this style, so apologies for inconsistencies along the way.


2024-03-23 12:21