# TypeError: 'EnvContext' object cannot be interpreted as an integer

**URL:** <https://discuss.ray.io/t/typeerror-envcontext-object-cannot-be-interpreted-as-an-integer/4083>\
**Category:** RLlib\
**Created:** [November 10, 2021, 5:57am UTC](https://discuss.ray.io/t/typeerror-envcontext-object-cannot-be-interpreted-as-an-integer/4083 "2021-11-10T05:57:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yinuo\_Du](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/yinuo_du/32/1709_2.png) [@Yinuo\_Du](https://discuss.ray.io/u/Yinuo_Du)\
**Post date:** [November 10, 2021, 5:57am UTC](https://discuss.ray.io/t/typeerror-envcontext-object-cannot-be-interpreted-as-an-integer/4083/1 "2021-11-10T05:57:03Z")

</div>

I’ve search the forum and saw previous posts on this issue. I tried the solutions suggested in those posts, however they didn’t work out.

 ![Screen Shot 2021-11-10 at 12.52.44 AM](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/e/e312c66fe5055534f0c7a986461bd815c5b2d450.png)

To replicate my error:

> from ray import tune  
> import gym, ray  
> from ray.rllib.agents import ppo  
> from envs.cybersec import CybersecEnv
> 
> # Register , how your env should be constructed (always with 5, or you can take values from the `config` EnvContext object):
> 
> tune.register\_env(“my\_env”, lambda config: CybersecEnv(2))
> 
> ray.init()  
> trainer = ppo.PPOTrainer(env=CybersecEnv, config={  
> “env\_config”: {  
> “num\_agents”: 2, # ← env\_config will be passed into your Env’s c’tor as `config`  
> },  
> })

Part of the implementation of my customized environment is as follows:

```auto
class CybersecEnv(gym.Env):
    def __init__ (self, num_agents=2, num_adversaries=1, num_edges=5,
                 num_vertices=4, num_bins=3, adversary_types=[], eta=1.0,
                 poss_adv_types=['w', 'p'], graph=None, p_obs=0.8, k=1,
                 def_budget=5):
        self.re_init(
            num_agents, num_adversaries, num_edges, num_vertices, num_bins,
            adversary_types, eta, poss_adv_types, graph, p_obs, k, def_budget
        )
        # TODO:
        # introduce defender budget for adjusting perceived target values
        # implement visualization for inspecting resulting policies
        # double check that the correct information is being conveyed to the specific attackers (w vs p)
    
    def re_init(self, num_agents, num_adversaries, num_edges, num_vertices,
                num_bins, adversary_types, eta, poss_adv_types, graph, p_obs,
                k, def_budget):
        self.num_agents = num_agents
        self.num_adversaries = num_adversaries 
        self.num_edges = num_edges
        self.num_vertices = num_vertices
        self.poss_adv_types = poss_adv_types
        self.eta = eta 
        self.p_obs = p_obs
        self.k = k
        self.n = num_agents
        self.def_budget = def_budget
        self.remaining_budget = def_budget
        if not adversary_types:
            self.adversary_types = np.random.choice(
                np.asarray(poss_adv_types), 
                size=num_adversaries,
                p=np.asarray([eta, 1-eta])
            )
        else:
            self.adversary_types = adversary_types
        self.num_bins = num_bins # number of bins for discretizing target values
        self.action_space = self.init_action_space()
        self.observation_space = self.init_obs_space()
        self.obs_shapes = [self.observation_space[i].shape for i in range(self.num_agents)]
        self.graph, self.vals = self.init_graph(graph)
        print(self.graph)
        self.entry_pts = self.get_entry_points()
        self.reset()

        # rendering 
        self.locs = []
        overlapping = True # TODO implement additional overlapping test
        overlap_diff = 0.5 # TODO test overlapping 
        while len(self.locs) < self.num_vertices:
            xpos = np.random.choice(np.arange(-4, 4))
            ypos = np.random.choice(np.arange(-4, 4))
            if (xpos, ypos) not in self.locs:
                self.locs.append((xpos, ypos))
        self.shared_viewer = True 
        if self.shared_viewer:
            self.viewers = [None]
        self.reset_render()

```

Could you please help see why this error occurs? I feel not quite appropriate to share the full implementation in the forum, are there any other more private communication channel? Thanks!

---

<div class="post-metadata">

**Author:** ![Lars\_Simon\_Zehnder](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/lars_simon_zehnder/32/1185_2.png) [@Lars\_Simon\_Zehnder](https://discuss.ray.io/u/Lars_Simon_Zehnder)\
**Post date:** [November 10, 2021, 2:12pm UTC](https://discuss.ray.io/t/typeerror-envcontext-object-cannot-be-interpreted-as-an-integer/4083/2 "2021-11-10T14:12:37Z")

</div>

Hi @Yinuo_Du ,

and welcome to the ray community. Just a suggestion after reading @sven1977 answer: If you want to use an `env_config` for your environment, you might need to define this in your c’tor of it like this:

```auto
class MyEnv(gym.Env):
    def __init__ (self, env_config=None): 
         super(MyEnv, self). __init__ ()
         self.config = env_config or {}
         self.var1 = self.config.get("var1", 0.0)
         ...

```

Hope I could help here.  
Simon

---

<div class="post-metadata">

**Author:** ![Yinuo\_Du](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/yinuo_du/32/1709_2.png) [@Yinuo\_Du](https://discuss.ray.io/u/Yinuo_Du)\
**Post date:** [November 10, 2021, 2:47pm UTC](https://discuss.ray.io/t/typeerror-envcontext-object-cannot-be-interpreted-as-an-integer/4083/3 "2021-11-10T14:47:24Z")

</div>

Thanks for your answer! Do I need to do the same in the re\_init() function?

---

<div class="post-metadata">

**Author:** ![Lars\_Simon\_Zehnder](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/lars_simon_zehnder/32/1185_2.png) [@Lars\_Simon\_Zehnder](https://discuss.ray.io/u/Lars_Simon_Zehnder)\
**Post date:** [November 10, 2021, 3:18pm UTC](https://discuss.ray.io/t/typeerror-envcontext-object-cannot-be-interpreted-as-an-integer/4083/4 "2021-11-10T15:18:18Z")

</div>

I can only guess that you mean the `reset()` method and no only in the ` __init__ ()`.
