I am wondering if there are more elegant solutions than the second to last comment on this Github issues thread:
opened 04:41AM - 12 Feb 20 UTC
closed 12:49PM - 12 Feb 20 UTC
question
## 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?
Yes this is great! I got confused reading old stackoverflow posts and github issues talking about PythonPath…
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.
Done! Great idea, thanks a lot for the suggestion.