# More elegant solution for importing files

**URL:** <https://discuss.ray.io/t/more-elegant-solution-for-importing-files/4708>\
**Category:** Ray Core\
**Created:** [January 14, 2022, 10:11pm UTC](https://discuss.ray.io/t/more-elegant-solution-for-importing-files/4708 "2022-01-14T22:11:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marsupialtail](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/marsupialtail/32/1950_2.png) [@marsupialtail](https://discuss.ray.io/u/marsupialtail)\
**Post date:** [January 14, 2022, 10:11pm UTC](https://discuss.ray.io/t/more-elegant-solution-for-importing-files/4708/1 "2022-01-14T22:11:55Z")

</div>

I am wondering if there are more elegant solutions than the second to last comment on this Github issues thread:

> <https://github.com/ray-project/ray/issues/7133>
>
> \## Evironment
> \- python version: 3.7.6
> \- ray version: 0.8.1
> \- OS of driver: ub…untu 18.04
> \- OS of worker: ubuntu 18.04
> 
> Hi, I met a problem recently when I was using ray. I want to let my remote function run in the remote node. This function was not written in the main script file but in a separate file. However, I get a exception said: \`This function was not imported properly\`.
> 
> This is what my script is written:
> \`\`\`python
> \## module-test.py
> 
> import ext\_module
> import ray
> 
> @ray.remote
> def other\_module():
> return ext\_module.cac\_sqrt3()
> 
> if \_\_name\_\_ == '\_\_main\_\_':
> ray.init(address='auto', redis\_password='5241590000000000')
> res = other\_module.remote()
> print("result is:", ray.get(res))
> 
> \## ext\_module.py
> 
> def cac\_sqrt3():
> return 1.732
> \`\`\`
> 
> I build a cluster of three nodes (one head node and two other nodes),
> to make sure the task is not run in the local machine when I connect the cluster, I set num-cpus=0
> \`\`\`
> ray start --address='xxxxxxx' --redis-password='5241590000000000' --num-cpus=0
> \`\`\`
> 
> 
> 
> 
> This is what the error log like:
> \`\`\`
> 2020-02-12 12:12:23,627	WARNING worker.py:1063 -- Failed to unpickle the remote function '\_\_main\_\_.other\_module' with function ID bb6e6179f5b2ef0eb388e72b4492bf508bc9ed39. Traceback:
> Traceback (most recent call last):
> File "/home/xxx/miniconda3/envs/ray\_dev/lib/python3.7/site-packages/ray/function\_manager.py", line 402, in fetch\_and\_register\_remote\_function
> function = pickle.loads(serialized\_function)
> File "/home/xxx/miniconda3/envs/ray\_dev/lib/python3.7/site-packages/ray/cloudpickle/cloudpickle.py", line 1136, in subimport
> \_\_import\_\_(name)
> ModuleNotFoundError: No module named 'ext\_module'
> 
> 
> Traceback (most recent call last):
> File "module-test.py", line 20, in \<module\>
> print("result is:", ray.get(res))
> File "/home/xxx/anaconda3/envs/ray\_dev2/lib/python3.7/site-packages/ray/worker.py", line 1492, in get
> raise value.as\_instanceof\_cause()
> ray.exceptions.RayTaskError: ray::\_\_main\_\_.other\_module() (pid=16330, ip=xxxxxx)
> File "python/ray/\_raylet.pyx", line 640, in ray.\_raylet.execute\_task
> Exception: This function was not imported properly.
> \`\`\`
> 
> What should I do to make it run normally?

I have a module A.py which I need in B.py which defines some Ray actors. Adding A.py to the PYTHONPATH does not work. Moving A.py source code into B.py works.

What is the best strategy to handle this kind of import in Ray right now?

---

<div class="post-metadata">

**Author:** ![simon-mo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/simon-mo/32/9_2.png) [@simon-mo](https://discuss.ray.io/u/simon-mo)\
**Post date:** [January 14, 2022, 11:07pm UTC](https://discuss.ray.io/t/more-elegant-solution-for-importing-files/4708/2 "2022-01-14T23:07:48Z")

</div>

Does [Handling Dependencies — Ray v1.9.2](https://docs.ray.io/en/latest/handling-dependencies.html) help?

---

<div class="post-metadata">

**Author:** ![marsupialtail](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/marsupialtail/32/1950_2.png) [@marsupialtail](https://discuss.ray.io/u/marsupialtail)\
**Post date:** [January 14, 2022, 11:19pm UTC](https://discuss.ray.io/t/more-elegant-solution-for-importing-files/4708/3 "2022-01-14T23:19:07Z")

</div>

Yes this is great! I got confused reading old stackoverflow posts and github issues talking about PythonPath…

---

<div class="post-metadata">

**Author:** ![architkulkarni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/architkulkarni/32/10_2.png) [@architkulkarni](https://discuss.ray.io/u/architkulkarni)\
**Post date:** [January 14, 2022, 11:48pm UTC](https://discuss.ray.io/t/more-elegant-solution-for-importing-files/4708/4 "2022-01-14T23:48:56Z")

</div>

Looks like Simon beat me to the punch here! @marsupialtail we’re trying to make this feature more prominent/discoverable in the docs, please let us know if you can think of other places in the Ray docs where we should add inbound links to this page.

---

<div class="post-metadata">

**Author:** ![marsupialtail](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/marsupialtail/32/1950_2.png) [@marsupialtail](https://discuss.ray.io/u/marsupialtail)\
**Post date:** [January 14, 2022, 11:50pm UTC](https://discuss.ray.io/t/more-elegant-solution-for-importing-files/4708/5 "2022-01-14T23:50:04Z")

</div>

Perhaps you guys can start by linking this doc page to all the places I found, like: [Import not working in a cluster · Issue #3116 · ray-project/ray · GitHub](https://github.com/ray-project/ray/issues/3116)  
[How to submit a task which is implemented in a separate module? · Issue #7133 · ray-project/ray · GitHub](https://github.com/ray-project/ray/issues/7133)  
[Parallel: Import a python file from sibling folder - Stack Overflow](https://stackoverflow.com/questions/54338013/parallel-import-a-python-file-from-sibling-folder)

---

<div class="post-metadata">

**Author:** ![architkulkarni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/architkulkarni/32/10_2.png) [@architkulkarni](https://discuss.ray.io/u/architkulkarni)\
**Post date:** [January 15, 2022, 12:01am UTC](https://discuss.ray.io/t/more-elegant-solution-for-importing-files/4708/6 "2022-01-15T00:01:36Z")

</div>

Done! Great idea, thanks a lot for the suggestion.
