Webots User Guide - chapter 4 - section 6

Webots User Guide


4.6 Using Python

4.6.1 Introduction

The Python API has been generated from the C++ API by using SWIG. That implies that their class hierarchy, their class names and their function names are almost identical. The Python API is currently composed of a set of about 25 classes having about 200 public functions located in the module called controller. The classes are either representations of a node of the scene tree (such as Robot, LED, etc.) or either utility classes (such as Motion, ImageRef, etc.). A complete description of these functions can be found in the reference guide while the instructions about the common way to program a Python controller can be found in chapter 6.

4.6.2 Python Installation

Version

The Python API of Webots is built with Python 2.7. Python 2.7 or earlier versions are therefore recommended although more recent versions can work without guarantee. Python 3 is not supported.

Mac OS X and Linux Instructions

Most of the Linux distribution have Python already installed. Same thing for the Macs. However, we recommend you to check your Python version and to upgrade Python if its version is under 2.7. In order to find the Python executable, Webots parses the directories of the PATH environment variable and looks for python2.* executable files. If your Python executable file is accessible from a command prompt, it implies that Python is correctly installed. You can check that by executing this command:

$ python2.X

where the X character is replaced by an integer greater than or equal to 6. If Python is not well installed, this command should inform you that the executable is not found. In this case, check first that Python 2.7 is installed, then check that the executable called python2.7 is somewhere (typically /usr/bin/python2.7) and finally check that the PATH environment variable includes the path to the Python executable. More information on the Python official web site.

Windows Instructions

For the Windows users, you need to install Python on your computer by getting and executing the right graphical installer which can be downloaded from the Python official web site. Make sure you have selected the 32 bit version since webots is incompatible with the 64 bit version. After the installation, you will need to set or change your PATH environment variable so that Webots is able to access the python command.

The PATH variable must be set using the Environment Variables dialog. On Windows XP, this dialog can be opened like this: Choose Start, Settings, Control Panel, and double-click System . Select the Advanced tab and then Environment Variables.

On Windows Vista, the dialog can be opened like this: Choose Start, Computer, System Properties, Advanced system settings, Advanced tab and then Environment Variables.

In the dialog, in the User variables for ... section, look for a variable named PATH. Add the bin path of Python to the right end of the list of paths. If the PATH variable does not exist you should create it. A typical value for PATH is:

C:\PythonXX\bin

Where XX stands for the version of Python.

Then, you need to restart Webots so that the change is taken into account.

You can check the Python version by executing this command in a command prompt:

$ python --version

This command should display the Python version. If Python is not installed (or not correctly installed), this command should inform you that python is not found. More information on the Python official web site.

4.6.3 Source Code of the Python API

For advanced users who want to modify the Python API, the SWIG script (controller.i), and the Makefile are located in the projects/languages/python/src directory while the generated library is located in the lib.

release 7.0.2
Copyright © 2012 Cyberbotics Ltd. All right reserved.