Accessing custom properties from personalized OpenAI gym environment

I’m working with a personalized gym environment that functions properly during RL model training. The issue is that when I add a custom property to my environment class, I can’t access it from another file.

Here’s my environment setup:

# my_env.py
class MyEnvironment(gym.Env):
    """Custom gym environment implementation"""

    def __init__(self, data, **options):
        # initialization code
        pass

    def reset(self):
        self.tracking_data = {"score1": 0, "score2": 0}
        return self._get_observation()

The tracking_data dictionary stores performance metrics during environment episodes.

In my training file:

# model_training.py
from my_env import MyEnvironment
# model training logic here
results = []
env_data = training_env.get_attr("tracking_data")
results.append(env_data["score1"])  # this line throws an error

I understand that accessing custom attributes requires the get_attr method, but I’m getting TypeError: list indices must be integers or slices, not str when trying to access env_data["score1"]. This suggests get_attr returns a list rather than the expected dictionary.

Should I be using set_attr somewhere in my code? What’s the correct approach to retrieve custom attributes from gym environments?

This happens because get_attr returns a list - even with one environment, most frameworks wrap everything in vectorized containers. Try env_data = training_env.get_attr("tracking_data")[0] then results.append(env_data["score1"]). Better yet, just add a method to your environment class like get_tracking_score(self, key) that returns self.tracking_data[key] directly. Way cleaner and skips the attribute mess entirely. Also make sure you’ve called reset() at least once before accessing tracking_data - I’ve hit this same issue when trying to grab custom properties before the environment’s actually initialized.