# Pytorch Import Errors

**URL:** https://forum.crunchdao.com/t/pytorch-import-errors/1144
**Category:** ADIA Lab
**Created:** [May 12, 2026, 9:19pm UTC](https://forum.crunchdao.com/t/pytorch-import-errors/1144 "2026-05-12T21:19:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![eventual-max](https://forum.crunchdao.com/user_avatar/forum.crunchdao.com/eventual-max/32/911_2.png) [@eventual-max](https://forum.crunchdao.com/u/eventual-max)
#### Post date: [May 12, 2026, 9:19pm UTC](https://forum.crunchdao.com/t/pytorch-import-errors/1144/1 "2026-05-12T21:19:41Z")

</div>

Hi, sorry this maybe a newbie question, but when I try to use pytorch in the submitted notebook, I keep getting torch import error. I see that torch is in the recommended package for the whitelist, and I did also tried `%pip install torch` before the train function, but still not able to import. Just wondering what is the best practice for use to use pytorch in the training, or for other packages as well. Thanks!

---

<div class="post-metadata">

### Author: ![enzo](https://forum.crunchdao.com/user_avatar/forum.crunchdao.com/enzo/32/15_2.png) [@enzo](https://forum.crunchdao.com/u/enzo)
#### Post date: [May 12, 2026, 10:04pm UTC](https://forum.crunchdao.com/t/pytorch-import-errors/1144/2 "2026-05-12T22:04:21Z")

</div>

Are you perhaps using torch as a transitive dependency? One that isn’t directly imported but used internally by another package?

If so, even if left unused, still import it.  
See [“Optional Dependencies” in the documentation](https://docs.crunchdao.com/competitions/participate/notebook-processor#optional-dependencies).

We cannot reliability parse magic commands (like `%pip`), but we should emit a warning so you are more aware that it’s not gonna work.

Apologies for the confusion.

---

<div class="post-metadata">

### Author: ![eventual-max](https://forum.crunchdao.com/user_avatar/forum.crunchdao.com/eventual-max/32/911_2.png) [@eventual-max](https://forum.crunchdao.com/u/eventual-max)
#### Post date: [May 12, 2026, 11:05pm UTC](https://forum.crunchdao.com/t/pytorch-import-errors/1144/3 "2026-05-12T23:05:27Z")

</div>

Thanks for the prompt reply. I just read through abunch of other posts, and want to confirm if this is true:

1. To use torch, we need to import them before the infer and train function, not inside any of those functions.
2. %pip install won’t work anyway, so I assume the torch is already installed in the environment

Thanks

---

<div class="post-metadata">

### Author: ![enzo](https://forum.crunchdao.com/user_avatar/forum.crunchdao.com/enzo/32/15_2.png) [@enzo](https://forum.crunchdao.com/u/enzo)
#### Post date: [May 12, 2026, 11:22pm UTC](https://forum.crunchdao.com/t/pytorch-import-errors/1144/4 "2026-05-12T23:22:22Z")

</div>

1. Yes, never `import` inside a function.  
The system will actually show you a warning, but it still works for specific cases if you really need it.

2. Not quite: we use `import` statements to then generate a `requirements.txt` file, which is pip-installed before we run your model.  
So if we detect a `import torch` (or a `from torch import abc`), `torch` will automatically be detected and then installed.  
That is why we have a “Preparing Python environment” when you submit. We are actually [running the converter](https://github.com/crunchdao/crunch-convert) in your browser ([yes really!](https://pyodide.org/en/stable/)), so you can see the result, and confirm (or edit) the file that are about to be included in your submission.

You can learn more sightly above [in the documentation](https://docs.crunchdao.com/competitions/participate/notebook-processor#specifying-package-versions).
