Custon Env works in local PC but not in Google Colab

I’m trying to run my RLlib custom environment in Google Colab. I tried first in my computer localy and all works good, but now in the Colab Notebook I have the following error:

ValueError                                Traceback (most recent call last)
<ipython-input-14-13576f35241d> in <cell line: 10>()
      9 # Regiter of the environment in RLlib (not compatible with Gym regiter)
---> 10 register_env(
     11   "EPEnv",
     12   lambda args: EnergyPlusEnv_v0(args)

4 frames
/usr/local/lib/python3.10/dist-packages/ray/tune/ in register_env(name, env_creator)
    131     if not callable(env_creator):
    132         raise TypeError("Second argument must be callable.", env_creator)
--> 133     _global_registry.register(ENV_CREATOR, name, env_creator)

/usr/local/lib/python3.10/dist-packages/ray/tune/ in register(self, category, key, value)
    235                 "Unknown category {} not among {}".format(category, KNOWN_CATEGORIES)
    236             )
--> 237         self._to_flush[(category, key)] = pickle.dumps_debug(value)
    238         if _internal_kv_initialized():
    239             self.flush_values()

/usr/local/lib/python3.10/dist-packages/ray/cloudpickle/ in dumps_debug(obj, *args, **kwargs)
     37 def dumps_debug(obj, *args, **kwargs):
     38     try:
---> 39         return dumps(obj, *args, **kwargs)
     40     except (TypeError, PicklingError) as exc:
     41         if os.environ.get("RAY_PICKLE_VERBOSE_DEBUG"):

/usr/local/lib/python3.10/dist-packages/ray/cloudpickle/ in dumps(obj, protocol, buffer_callback)
     86         with io.BytesIO() as file:
     87             cp = CloudPickler(file, protocol=protocol, buffer_callback=buffer_callback)
---> 88             cp.dump(obj)
     89             return file.getvalue()

/usr/local/lib/python3.10/dist-packages/ray/cloudpickle/ in dump(self, obj)
    731     def dump(self, obj):
    732         try:
--> 733             return Pickler.dump(self, obj)
    734         except RuntimeError as e:
    735             if "recursion" in e.args[0]:

ValueError: ctypes objects containing pointers cannot be pickled

Any suggestion on how to fix it? Thanks in advance!!

@hermmanhender , in case we are writing data structures in C, we are responsible to define how the structure should be stored. In this case there appears to be a pointer that points to a data structure, but that structure is not in the state when pickling. In such a case we have to write a __setstate__ (unpickling) and __getstate__/__reduce__ (pickling).

Hi @Lars_Simon_Zehnder sorry the delay.

I tried with your suggestion but still did not work.

I solve the problem saving my environment configuration in a GitHub repository and the cloning the repository in Google Colab to acces to the environment files:

# clone the public repository
!git clone

# add the repository to the path
import sys

Finally, I use the following code to check the serialization:

from ray.util import inspect_serializability
from my_repository.env_folder import custom_env

is_serializable, unserializable_objects = inspect_serializability(custom_env, depth=10)
print(f"Is serializable: {is_serializable}")
if not is_serializable:
    print("Unserializable objects:")
    for obj in unserializable_objects:

and the exit was:

Checking Serializability of <class 'my_repository.env_folder.custom_env'>
Is serializable: True
1 Like