# Using ray in when imported as module and run as module

**URL:** <https://discuss.ray.io/t/using-ray-in-when-imported-as-module-and-run-as-module/13251>\
**Category:** Ray Core\
**Created:** [December 23, 2023, 2:47pm UTC](https://discuss.ray.io/t/using-ray-in-when-imported-as-module-and-run-as-module/13251 "2023-12-23T14:47:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sral](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sral/32/4730_2.png) [@Sral](https://discuss.ray.io/u/Sral)\
**Post date:** [December 23, 2023, 2:47pm UTC](https://discuss.ray.io/t/using-ray-in-when-imported-as-module-and-run-as-module/13251/1 "2023-12-23T14:47:15Z")

</div>

**How severe does this issue affect your experience of using Ray?**

- High: It blocks me to complete my task.

Hello guys,

I have a problem using ray in the context of using it as a module.

I have the following code ( **main**.py), which is a simplified version of the actual file, but it has the same structure and error messages:

```
import ray
import importlib
from pathlib import Path
import sys
import os

try:
     from parallel.rayParallel import mainWrapper
except ImportError:
     from rayParallel import mainWrapper

if __name__ == " __main__":
     if not ray.is_initialized():
         ray.init()
     path = Path("path/to/python_script")
    
     sys.path.insert(0, f"{path.parent}{os.sep}")
     func = importlib.import_module(path.stem)
     run_func = func.mainWrapper
     run_func()

```

It initializes ray and than interanalizes my simulation script as a module and than runs the “mainWrapper” function which actually does the computation and parallelization with ray.

The thing is, when I run this script as a normal python script (python3 **main**.py) it works just fine.

But when I want to run it as a module (which is my ultimate goal) with  
python3 -m parallel (the test module is named parallel, which has a **init**.py and the above **main**.py) function, I get the following error:

```
RuntimeError: The remote function failed to import on the worker. This may be because needed library dependencies are not installed in the worker environment:

ray::parallel() (pid=96716, ip=134.61.193.179)
ModuleNotFoundError: No module named 'rayParallel'

```

I already tried debugging, searched the internet, but did not find any solution.

Thank in advance 🙂

---

<div class="post-metadata">

**Author:** ![jjyao](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/jjyao/32/1799_2.png) [@jjyao](https://discuss.ray.io/u/jjyao)\
**Post date:** [December 24, 2023, 2:50am UTC](https://discuss.ray.io/t/using-ray-in-when-imported-as-module-and-run-as-module/13251/2 "2023-12-24T02:50:38Z")

</div>

Could you make sure `rayParallel` can be found from the `sys.path`?

---

<div class="post-metadata">

**Author:** ![Sral](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sral/32/4730_2.png) [@Sral](https://discuss.ray.io/u/Sral)\
**Post date:** [December 24, 2023, 2:16pm UTC](https://discuss.ray.io/t/using-ray-in-when-imported-as-module-and-run-as-module/13251/3 "2023-12-24T14:16:32Z")

</div>

Yes, the file rayParallal.py is included in the sys.path, see line (sys.path.insert etc…)

---

<div class="post-metadata">

**Author:** ![Sral](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sral/32/4730_2.png) [@Sral](https://discuss.ray.io/u/Sral)\
**Post date:** [December 24, 2023, 2:22pm UTC](https://discuss.ray.io/t/using-ray-in-when-imported-as-module-and-run-as-module/13251/4 "2023-12-24T14:22:53Z")

</div>

> [@jjyao](#):
>
> Could you make sure `rayParallel` can be found from the `sys.path`?

I also checked the sys.path, and the folder where rayParallel.py (or later the module rayParallel) is included is contained in the systems path.

---

<div class="post-metadata">

**Author:** ![Sral](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sral/32/4730_2.png) [@Sral](https://discuss.ray.io/u/Sral)\
**Post date:** [December 29, 2023, 3:33pm UTC](https://discuss.ray.io/t/using-ray-in-when-imported-as-module-and-run-as-module/13251/5 "2023-12-29T15:33:46Z")

</div>

Does anyone have a solution?  
I still haven’t solved this problem.

---

<div class="post-metadata">

**Author:** ![Sral](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sral/32/4730_2.png) [@Sral](https://discuss.ray.io/u/Sral)\
**Post date:** [December 31, 2023, 2:54pm UTC](https://discuss.ray.io/t/using-ray-in-when-imported-as-module-and-run-as-module/13251/6 "2023-12-31T14:54:30Z")

</div>

anyone?  
I have been working on this problem for a week now, and I cannot continue without solving it.

---

<div class="post-metadata">

**Author:** ![Jules\_Damji](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/jules_damji/32/4058_2.png) [@Jules\_Damji](https://discuss.ray.io/u/Jules_Damji)\
**Post date:** [January 2, 2024, 5:40pm UTC](https://discuss.ray.io/t/using-ray-in-when-imported-as-module-and-run-as-module/13251/7 "2024-01-02T17:40:48Z")

</div>

@jjyao I wonder if making this part of runtime env in the ray.init() would help?

---

<div class="post-metadata">

**Author:** ![Sral](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sral/32/4730_2.png) [@Sral](https://discuss.ray.io/u/Sral)\
**Post date:** [January 2, 2024, 6:26pm UTC](https://discuss.ray.io/t/using-ray-in-when-imported-as-module-and-run-as-module/13251/8 "2024-01-02T18:26:02Z")

</div>

I fixed it, but it was quite an endeavour.

---

<div class="post-metadata">

**Author:** ![dhruvnigam93](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/dhruvnigam93/32/7267_2.png) [@dhruvnigam93](https://discuss.ray.io/u/dhruvnigam93)\
**Post date:** [November 6, 2024, 7:43pm UTC](https://discuss.ray.io/t/using-ray-in-when-imported-as-module-and-run-as-module/13251/9 "2024-11-06T19:43:41Z")

</div>

How? I am also facing this error.
