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!
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.
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.
Thanks for the prompt reply. I just read through abunch of other posts, and want to confirm if this is true:
- To use torch, we need to import them before the infer and train function, not inside any of those functions.
- %pip install won’t work anyway, so I assume the torch is already installed in the environment
Thanks
-
Yes, never
importinside a function.
The system will actually show you a warning, but it still works for specific cases if you really need it. -
Not quite: we use
importstatements to then generate arequirements.txtfile, which is pip-installed before we run your model.
So if we detect aimport torch(or afrom torch import abc),torchwill automatically be detected and then installed.
That is why we have a “Preparing Python environment” when you submit. We are actually running the converter in your browser (yes really!), 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.