# ModuleNotFoundError for custom env import when num\_workers \> 0

**URL:** <https://discuss.ray.io/t/modulenotfounderror-for-custom-env-import-when-num-workers-0/3950>\
**Category:** Ray Core\
**Created:** [October 27, 2021, 4:16pm UTC](https://discuss.ray.io/t/modulenotfounderror-for-custom-env-import-when-num-workers-0/3950 "2021-10-27T16:16:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![klausk55](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@klausk55](https://discuss.ray.io/u/klausk55)\
**Post date:** [October 27, 2021, 4:16pm UTC](https://discuss.ray.io/t/modulenotfounderror-for-custom-env-import-when-num-workers-0/3950/1 "2021-10-27T16:16:56Z")

</div>

Hi all,

In the config of my PPOTrainer I had set `"num_workers": 0` so far, but when I change it to `num_workers` \> 0 then it raises a _ModuleNotFoundError: No module named …_ for the import of my custom env. Is this issue the same as mentioned [here](https://github.com/ray-project/ray/issues/5311)?

Yet, I’m not familiar with all the effects of `num_workers` \> 0. I’m working on a single machine (desktop computer) and don’t know if `num_workers` \> 0 works in this case anyway? Or is it just possible setting for remote workers running on multiple machines?  
Some enlightening thoughts are really appreciated.

> The actor died because of an error raised in its creation task, e[36mray::RolloutWorker. **init** ()e[39m (pid=16960, ip=192.168.250.100)  
> File “python\ray\_raylet.pyx”, line 460, in ray.\_raylet.execute\_task  
> File “python\ray\_raylet.pyx”, line 481, in ray.\_raylet.execute\_task  
> File “python\ray\_raylet.pyx”, line 351, in ray.\_raylet.raise\_if\_dependency\_failed  
> ray.exceptions.RaySystemError: System error: No module named ‘galvcon’  
> traceback: Traceback (most recent call last):  
> File “C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\ray\serialization.py”, line 248, in deserialize\_objects  
> obj = self.\_deserialize\_object(data, metadata, object\_ref)  
> File “C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\ray\serialization.py”, line 190, in \_deserialize\_object  
> return self.\_deserialize\_msgpack\_data(data, metadata\_fields)  
> File “C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\ray\serialization.py”, line 168, in \_deserialize\_msgpack\_data  
> python\_objects = self.\_deserialize\_pickle5\_data(pickle5\_data)  
> File “C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\ray\serialization.py”, line 158, in \_deserialize\_pickle5\_data  
> obj = pickle.loads(in\_band)  
> ModuleNotFoundError: No module named ‘galvcon’

---

<div class="post-metadata">

**Author:** ![mannyv](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mannyv/32/606_2.png) [@mannyv](https://discuss.ray.io/u/mannyv)\
**Post date:** [October 30, 2021, 12:17pm UTC](https://discuss.ray.io/t/modulenotfounderror-for-custom-env-import-when-num-workers-0/3950/2 "2021-10-30T12:17:33Z")

</div>

Hi @klausk55,

Yes you can run with more than 1 worker even on a single machine.

Each worker runs in a separate process. What you are likely seeing is that the workers do not have the same path or environment variable set and cannot find your module.

Try asking in the core category.

---

<div class="post-metadata">

**Author:** ![klausk55](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@klausk55](https://discuss.ray.io/u/klausk55)\
**Post date:** [November 2, 2021, 10:18am UTC](https://discuss.ray.io/t/modulenotfounderror-for-custom-env-import-when-num-workers-0/3950/3 "2021-11-02T10:18:29Z")

</div>

Now my question reassigned to the hopefully appropriate category.

How to fix what @mannyv identified as the problem?

> [@mannyv](#):
>
> Each worker runs in a separate process. What you are likely seeing is that the workers do not have the same path or environment variable set and cannot find your module.

---

<div class="post-metadata">

**Author:** ![TanjaBayer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/tanjabayer/32/1288_2.png) [@TanjaBayer](https://discuss.ray.io/u/TanjaBayer)\
**Post date:** [November 2, 2021, 8:36pm UTC](https://discuss.ray.io/t/modulenotfounderror-for-custom-env-import-when-num-workers-0/3950/4 "2021-11-02T20:36:07Z")

</div>

You are mentoining custom env, this is likely to be related. Can you give an example how you create this env?

---

<div class="post-metadata">

**Author:** ![klausk55](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@klausk55](https://discuss.ray.io/u/klausk55)\
**Post date:** [November 3, 2021, 9:00am UTC](https://discuss.ray.io/t/modulenotfounderror-for-custom-env-import-when-num-workers-0/3950/5 "2021-11-03T09:00:16Z")

</div>

@TanjaBayer I have a file called _main.py_ where I import the custom RL env, i.e. `from my_module.sub_folder.sub_sub_folder.my_env import MyEnv`. In _main.py_ I create a RL Trainer which is given this custom env. When I have `"num_workers": 0` in the Trainer config then everything works fine since there is only a “local worker”, but with `"num_workers" > 0` (i.e. “local worker” + “remote workers”) the error occurs.  
As a first workaround I copied the folder of `my_module` into the `rllib` folder and changed the import in _main.py_: `from ray.rllib.my_module.sub_folder.sub_sub_folder.my_env import MyEnv`

Is this what you wanted to know?

---

<div class="post-metadata">

**Author:** ![miguelinux314](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/miguelinux314/32/5683_2.png) [@miguelinux314](https://discuss.ray.io/u/miguelinux314)\
**Post date:** [January 31, 2024, 4:10pm UTC](https://discuss.ray.io/t/modulenotfounderror-for-custom-env-import-when-num-workers-0/3950/6 "2024-01-31T16:10:29Z")

</div>

@klausk55 I think I run into a similar problem.

Remote workers would fail to deserialize data from modules imported from within the project. In my case, it was due to the deserialization process being attempted from the workers’ initial directory instead of the working dir. I could fix this issue by running a worker\_process\_setup\_hook function before submitting tasks to the workers. This function would perform an os.chdir so that the (relative) imports work well:

```auto
ray.init(runtime_env={"worker_process_setup_hook": logging_setup_func})

```

I got the idea from [Execute function on each worker process in the cluster](https://discuss.ray.io/t/execute-function-on-each-worker-process-in-the-cluster/12314)

I didn’t find a explicit solution to my problem, so maybe this will help others as well.

---

<div class="post-metadata">

**Author:** ![klausk55](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@klausk55](https://discuss.ray.io/u/klausk55)\
**Post date:** [February 15, 2024, 2:41pm UTC](https://discuss.ray.io/t/modulenotfounderror-for-custom-env-import-when-num-workers-0/3950/7 "2024-02-15T14:41:15Z")

</div>

@miguelinux314 to be honest, I cannot remember the details and what the conclusio was 😅 😅

I’m not sure, but maybe this might did it for me:

```auto
    # initialize Ray and distribute package "mymodulewithcustomenv"
    runtime_env = {"py_modules": [mymodulewithcustomenv]}
    ray.init(log_to_driver=False, runtime_env=runtime_env)

```
