Skip to content

[BUG] MuJoCo engine import hides the error #6

@ShravanTata

Description

@ShravanTata

Problem Description

The current import handling for MuJoCo and Bullet engines masks actual import errors that occur within the farms_mujoco and farms_bullet modules. This makes debugging dependency issues difficult.

https://github.com/farmsim/farms_sim/blob/ccd742b292849748dc8e9819b3d5bb9becc25ff3/farms_sim/simulation.py#L15

Current Code

ENGINE_MUJOCO = False
try:
    from farms_mujoco.simulation.simulation import (
        Simulation as MuJoCoSimulation,
    )
    ENGINE_MUJOCO = True
except ImportError:
    MuJoCoSimulation = None

ENGINE_BULLET = False
try:
    from farms_bullet.simulation.simulation import (
        AnimatSimulation as PybulletSimulation
    )
    ENGINE_BULLET = True
except ImportError:
    PybulletSimulation = None

if not ENGINE_MUJOCO and not ENGINE_BULLET:
    raise ImportError('Neither MuJoCo nor Bullet are installed')

Issue

When farms_mujoco or farms_bullet packages are installed but have internal dependency issues (missing libraries, version mismatches, etc.), the broad except ImportError catches these errors and treats them as if the packages aren't installed at all. This leads to:

  1. Silent failures: Real configuration problems are hidden
  2. Misleading error messages: Users get "Neither MuJoCo nor Bullet are installed" instead of the actual error
  3. Difficult debugging: Developers can't see what's actually wrong

Expected Behavior

The code should distinguish between:

  • Package not installed (expected case)
  • Package installed but failing to import due to internal errors (needs user attention)

Proposed Solution

Approach using ModuleNotFoundError :

ENGINE_MUJOCO = False
try:
    from farms_mujoco.simulation.simulation import (
        Simulation as MuJoCoSimulation,
    )
    ENGINE_MUJOCO = True
except ModuleNotFoundError:
    # Package not installed - this is expected
    MuJoCoSimulation = None
except ImportError as e:
    # Package exists but has internal import issues - re-raise with context
    raise ImportError(f"farms_mujoco is installed but failed to import: {e}") from e

ENGINE_BULLET = False
try:
    from farms_bullet.simulation.simulation import (
        AnimatSimulation as PybulletSimulation
    )
    ENGINE_BULLET = True
except ModuleNotFoundError:
    # Package not installed - this is expected  
    PybulletSimulation = None
except ImportError as e:
    # Package exists but has internal import issues - re-raise with context
    raise ImportError(f"farms_bullet is installed but failed to import: {e}") from e

if not ENGINE_MUJOCO and not ENGINE_BULLET:
    raise ImportError('Neither MuJoCo nor Bullet packages are installed')

Reproduction Steps

To reproduce the current issue:

  1. Install farms_mujoco with missing system dependencies
  2. Try to import - you'll get the generic "Neither MuJoCo nor Bullet are installed" message
  3. The actual dependency error is hidden

Metadata

Metadata

Labels

bugSomething isn't workingenhancementNew feature or request

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions