# ModuleNotFoundError for torch

**URL:** <https://discuss.ray.io/t/modulenotfounderror-for-torch/21157>\
**Category:** Ray Tune\
**Created:** [December 19, 2024, 3:36pm UTC](https://discuss.ray.io/t/modulenotfounderror-for-torch/21157 "2024-12-19T15:36:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahebeh.dadboud](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@sahebeh.dadboud](https://discuss.ray.io/u/sahebeh.dadboud)\
**Post date:** [December 19, 2024, 3:36pm UTC](https://discuss.ray.io/t/modulenotfounderror-for-torch/21157/1 "2024-12-19T15:36:48Z")

</div>

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

- High: It blocks me to complete my task.

I’m new to Ray. To try a sample code, I’m using [Fine-tune a Hugging Face Transformers Model — Ray 2.40.0](https://docs.ray.io/en/latest/train/examples/transformers/huggingface_text_classification.html) step by step. Everything looks good until I get to `trainer.fit()`, the error message I get is:

**ModuleNotFoundError: No module named ‘torch’.**

(I’ve added the full error at the of the message.)

I can simply` import torch`, or `import ray.train.torch.` The only problem is the `trainer.fit()`.

additional information:  
pytorch has been installed from root for all users, the path looks like this:  
\</opt/data/python/pytorch/venv/lib/python3.9/site-packages/\>  
version is: **2.5.1+cu124**  
ray version: **2.40.0**  
python version: **3.9**  
I’m running the codes in a jupyter notebook.

can you let me know what is the problem?

`trainer.fit()`

> * * *
> 
> ModuleNotFoundError Traceback (most recent call last)  
> Cell In[37], line 1  
> ----\> 1 trainer.fit()
> 
> File /opt/data/python/ray/venv/lib/python3.9/site-packages/ray/train/base\_trainer.py:580, in BaseTrainer.fit(self)  
> 577 from ray.tune import ResumeConfig, TuneError  
> 578 from ray.tune.tuner import Tuner  
> → 580 trainable = self.as\_trainable()  
> 581 param\_space = self.\_extract\_fields\_for\_tuner\_param\_space()  
> 583 self.run\_config.name = (  
> 584 self.run\_config.name or StorageContext.get\_experiment\_dir\_name(trainable)  
> 585 )
> 
> File /opt/data/python/ray/venv/lib/python3.9/site-packages/ray/train/base\_trainer.py:827, in BaseTrainer.as\_trainable(self)  
> 824 trainable\_cls = self.\_generate\_trainable\_cls()  
> 826 # Wrap with `tune.with_parameters` to handle very large values in base\_config  
> → 827 return tune.with\_parameters(trainable\_cls, \*\*base\_config)
> 
> File /opt/data/python/ray/venv/lib/python3.9/site-packages/ray/tune/trainable/util.py:107, in with\_parameters(trainable, \*\*kwargs)  
> 105 prefix = f"{str(trainable)}\_"  
> 106 for k, v in kwargs.items():  
> → 107 parameter\_registry.put(prefix + k, v)  
> 109 trainable\_name = getattr(trainable, “ **name** ”, “tune\_with\_parameters”)  
> 110 keys = set(kwargs.keys())
> 
> File /opt/data/python/ray/venv/lib/python3.9/site-packages/ray/tune/registry.py:301, in \_ParameterRegistry.put(self, k, v)  
> 299 self.to\_flush[k] = v  
> 300 if ray.is\_initialized():  
> → 301 self.flush()
> 
> File /opt/data/python/ray/venv/lib/python3.9/site-packages/ray/tune/registry.py:313, in \_ParameterRegistry.flush(self)  
> 311 self.references[k] = v  
> 312 else:  
> → 313 self.references[k] = ray.put(v)  
> 314 self.to\_flush.clear()
> 
> File /opt/data/python/ray/venv/lib/python3.9/site-packages/ray/\_private/auto\_init\_hook.py:21, in wrap\_auto\_init..auto\_init\_wrapper(\*args, \*\*kwargs)  
> 18 @wraps(fn)  
> 19 def auto\_init\_wrapper(\*args, \*\*kwargs):  
> 20 auto\_init\_ray()  
> —\> 21 return fn(\*args, \*\*kwargs)
> 
> File /opt/data/python/ray/venv/lib/python3.9/site-packages/ray/\_private/client\_mode\_hook.py:102, in client\_mode\_hook..wrapper(\*args, \*\*kwargs)  
> 98 if client\_mode\_should\_convert():  
> 99 # Legacy code  
> 100 # we only convert init function if RAY\_CLIENT\_MODE=1  
> 101 if func. **name**!= “init” or is\_client\_mode\_enabled\_by\_default:  
> → 102 return getattr(ray, func. **name** )(\*args, \*\*kwargs)  
> 103 return func(\*args, \*\*kwargs)
> 
> File /opt/data/python/ray/venv/lib/python3.9/site-packages/ray/util/client/api.py:52, in \_ClientAPI.put(self, \*args, \*\*kwargs)  
> 44 def put(self, \*args, \*\*kwargs):  
> 45 “”“put is the hook stub passed on to replace `ray.put`  
> 46  
> 47 Args:  
> (…)  
> 50 kwargs: opaque keyword arguments  
> 51 “””  
> —\> 52 return self.worker.put(\*args, \*\*kwargs)
> 
> File /opt/data/python/ray/venv/lib/python3.9/site-packages/ray/util/client/worker.py:495, in Worker.put(self, val, client\_ref\_id, \_owner)  
> 487 raise TypeError(  
> 488 "Calling ‘put’ on an ObjectRef is not allowed "  
> 489 "(similarly, returning an ObjectRef from a remote "  
> (…)  
> 492 “call ‘put’ on it (or return it).”  
> 493 )  
> 494 data = dumps\_from\_client(val, self.\_client\_id)  
> → 495 return self.\_put\_pickled(data, client\_ref\_id, \_owner)
> 
> File /opt/data/python/ray/venv/lib/python3.9/site-packages/ray/util/client/worker.py:509, in Worker.\_put\_pickled(self, data, client\_ref\_id, owner)  
> 507 if not resp.valid:  
> 508 try:  
> → 509 raise cloudpickle.loads(resp.error)  
> 510 except (pickle.UnpicklingError, TypeError):  
> 511 logger.exception(“Failed to deserialize {}”.format(resp.error))
> 
> ModuleNotFoundError: No module named ‘torch’

---

<div class="post-metadata">

**Author:** ![matthewdeng](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/matthewdeng/32/1446_2.png) [@matthewdeng](https://discuss.ray.io/u/matthewdeng)\
**Post date:** [December 19, 2024, 11:40pm UTC](https://discuss.ray.io/t/modulenotfounderror-for-torch/21157/2 "2024-12-19T23:40:11Z")

</div>

From the stacktrace it seems like you are using Ray Client, which may be causing the issue. Can you try running without Ray Client?

---

<div class="post-metadata">

**Author:** ![sahebeh.dadboud](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@sahebeh.dadboud](https://discuss.ray.io/u/sahebeh.dadboud)\
**Post date:** [December 20, 2024, 12:35pm UTC](https://discuss.ray.io/t/modulenotfounderror-for-torch/21157/3 "2024-12-20T12:35:19Z")

</div>

I’m using a server that has the ray installed for us as users. I want to have access to different nodes in our cluster and just simply train my model. Before doing this, I’ve started using the sample code provided in the post (get familiar with Ray). I don’t know how actually ray has been installed for us, since I am not the admin. But what would be the difference? If I’m running from Ray Client I should be able able have access to my cluster with ray.init() or the setup is different? I need the information to tell the admin installing it differently.

second attempt on my local machine: I got to start training (after so many solvable errors 😃 ). But I now get:

> “RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from [Download The Latest Official NVIDIA Drivers](http://www.nvidia.com/Download/index.aspx)”

Is that training sample only running on GPU? even though I’ve set gpu==False.
