Skip to content

Make examples importable when run from their own directory - #1521

Open
b84330808 wants to merge 1 commit into
AtsushiSakai:masterfrom
b84330808:fix-run-examples-from-own-directory
Open

b84330808 wants to merge 1 commit into
AtsushiSakai:masterfrom
b84330808:fix-run-examples-from-own-directory

Conversation

@b84330808

Copy link
Copy Markdown

Reference issue

Same problem as #966, #750 and #709. The latest comment on #750 (March 2026) still hits it with SLAM/FastSLAM2/fast_slam2.py.

What does this implement/fix?

"How to run sample codes" says to cd into an example directory and run python <script>.py. These examples import utils, Mapping or PathPlanning from the repository root without adding the root to sys.path, so they stop with ModuleNotFoundError before anything runs:

  • ArmNavigation/two_joint_arm_to_point_control/two_joint_arm_to_point_control.py
  • Mapping/ndt_map/ndt_map.py, Mapping/normal_vector_estimation/normal_vector_estimation.py
  • PathPlanning/ClosedLoopRRTStar/ (closed_loop_rrt_star_car.py, pure_pursuit.py, unicycle_model.py)
  • PathPlanning/ModelPredictiveTrajectoryGenerator/ (trajectory_generator.py, lookup_table_generator.py)
  • PathPlanning/TimeBasedPathPlanning/ (GridWithDynamicObstacles.py, PriorityBasedPlanner.py, SafeInterval.py, SpaceTimeAStar.py)
  • SLAM/EKFSLAM/ekf_slam.py, SLAM/FastSLAM1/fast_slam1.py, SLAM/FastSLAM2/fast_slam2.py
$ cd SLAM/EKFSLAM
$ python ekf_slam.py
    from utils.angle import angle_mod
ModuleNotFoundError: No module named 'utils'

This adds sys.path.append(str(pathlib.Path(__file__).parent.parent.parent)) before those imports, the same fix #1188 made for the PathTracking examples. For unicycle_model.py and motion_model.py the line goes in the module itself, so every script that imports them works.

The unit tests didn't catch this because tests/conftest.py puts the root on sys.path.

Additional information

tests/test_run_from_script_directory.py loads each of the scripts above in a fresh interpreter, with the script's directory as the working directory and without the root on sys.path. It uses runpy.run_path, so only the imports run, not the simulations. All 15 cases fail on master and pass with this change.

I found these by running every example the way the docs describe, from its own directory. After this change all of the scripts above get past their imports. ndt_map.py then stops with the separate #991 bug ('NDTMap' object has no attribute 'grid_map'), which this PR doesn't touch.

Ran on Windows 11, Python 3.13 with the pinned requirements (ecos left out, it has no Windows wheel for 3.13):

  • pytest tests/test_run_from_script_directory.py -Werror: 15 passed (15 failed with the source changes reverted)
  • tests for the touched examples, plus test_codestyle.py and test_mypy_type_check.py: passed

CheckList

  • Did you add an unittest for your new example or defect fix?
  • Did you add documents for your new example?
  • All CIs are green? (You can check it after submitting)

The docs say to cd into an example directory and run `python <script>.py`.
These examples import `utils`, `Mapping` or `PathPlanning` from the
repository root without adding it to sys.path, so they stop with
ModuleNotFoundError before the simulation starts:

- ArmNavigation two_joint_arm_to_point_control
- Mapping ndt_map and normal_vector_estimation
- PathPlanning ClosedLoopRRTStar (closed_loop_rrt_star_car, pure_pursuit,
  unicycle_model) and ModelPredictiveTrajectoryGenerator
  (trajectory_generator, lookup_table_generator)
- PathPlanning TimeBasedPathPlanning (GridWithDynamicObstacles,
  PriorityBasedPlanner, SafeInterval, SpaceTimeAStar)
- SLAM EKFSLAM, FastSLAM1 and FastSLAM2

Add the root to sys.path before those imports, as AtsushiSakai#1188 did for the
PathTracking examples. The new test loads each script in a fresh
interpreter from the script's directory, without the root on sys.path.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant