能否在Jupyter中运行Webots及如何完成安装配置?
Hey there! I totally get why you'd prefer Jupyter's cell-by-cell execution for working with Webots—it's perfect for testing and iterating code snippets quickly. Let me walk you through the full setup and configuration steps to get Webots running smoothly in Jupyter.
Prerequisites
- First, make sure you have Webots installed (any recent stable version works, but I recommend grabbing the latest release).
- Have Jupyter Notebook/Lab set up on your machine—install it via
pip install jupyterorpip install jupyterlabif you haven't already. - Double-check that your Python environment matches the version Webots uses (you can find this in Webots under
Help > About Webotsto avoid compatibility headaches).
Step 1: Locate Webots' Python Library Path
Webots ships with its own Python bindings, so we need to point Jupyter to their location:
- Windows: Typically
C:\Program Files\Webots\lib\controller\python - macOS: Look in
/Applications/Webots.app/Contents/lib/controller/python - Linux: Usually
/usr/local/webots/lib/controller/python(adjust if you installed Webots in a custom directory)
Step 2: Add Webots Path to Jupyter's Python Environment
You have two options here, depending on whether you want a temporary or permanent setup:
Option 1: Temporary Setup (per notebook session)
In the first cell of your Jupyter notebook, run these lines to append the Webots path for the current session only:
import sys # Replace with your actual Webots Python path webots_path = "/Applications/Webots.app/Contents/lib/controller/python" sys.path.append(webots_path)
You'll need to run this cell every time you open the notebook.
Option 2: Permanent Setup (all Jupyter sessions)
To avoid repeating the path setup, add it to your Python environment permanently:
- Create a text file named
webots.pth - Paste the full Webots Python library path into the file (just the raw path, no quotes or extra text)
- Move this
.pthfile to your Python environment'ssite-packagesdirectory:- Windows:
C:\PythonXX\Lib\site-packages(replace XX with your Python version) - macOS/Linux:
/usr/local/lib/pythonX.X/site-packages(or thesite-packagesfolder of your virtual environment if you use one)
- Windows:
Step 3: Test the Configuration
Create a new Jupyter notebook and run this simple test to confirm everything works:
from controller import Robot # Initialize the robot instance robot = Robot() # Get the simulation time step timestep = int(robot.getBasicTimeStep()) # Run a quick test loop while robot.step(timestep) != -1: print("Webots is running in Jupyter! 🎉") break # Stop after one iteration to avoid infinite loop in the notebook
If you see the printed message without import or runtime errors, you're all set!
Quick Workflow Tips
- Since Jupyter cells share the same context, you can initialize the
Robotobject in one cell and reuse it in subsequent cells for incremental testing. - If you hit import errors, double-check that your Webots path is correct and that your Python version matches what Webots supports.
- For longer simulations, JupyterLab tends to handle long-running cells better than the classic Jupyter Notebook interface.
内容的提问来源于stack exchange,提问作者Steve Brown

