Unable to import PyTorch in ROOT Notebook despite having installed package

Hello everyone,

I would like to import torch to do machine learning using TMVA. However, when I try to do so, I get a ModuleNotFound error. I am currently doing this in the Jupyter Notebook for ROOT. When I run

import sys, subprocess
subprocess.run([sys.executable, '-m', 'pip', 'list'])

to see what packages I have installed in the ROOT Notebook’s environment, this is my output

Package                      Version
---------------------------- ----------
absl-py                      1.4.0
anyio                        3.6.2
argon2-cffi                  21.3.0
argon2-cffi-bindings         21.2.0
arrow                        1.2.3
asttokens                    2.2.1
astunparse                   1.6.3
attrs                        22.2.0
backcall                     0.2.0
beautifulsoup4               4.11.2
bleach                       6.0.0
cachetools                   5.3.0
certifi                      2022.12.7
cffi                         1.15.1
charset-normalizer           3.1.0
comm                         0.1.2
contourpy                    1.0.7
cycler                       0.11.0
debugpy                      1.6.6
decorator                    5.1.1
defusedxml                   0.7.1
executing                    1.2.0
fastjsonschema               2.16.2
flatbuffers                  23.1.21
fonttools                    4.38.0
fqdn                         1.5.1
gast                         0.4.0
google-auth                  2.16.1
google-auth-oauthlib         0.4.6
google-pasta                 0.2.0
grpcio                       1.51.1
h5py                         3.8.0
idna                         3.4
ipykernel                    6.21.2
ipython                      8.10.0
ipython-genutils             0.2.0
ipywidgets                   8.0.4
isoduration                  20.11.0
jedi                         0.18.2
Jinja2                       3.1.2
joblib                       1.2.0
jsonpointer                  2.3
jsonschema                   4.17.3
jupyter                      1.0.0
jupyter_client               8.0.3
jupyter-console              6.5.1
jupyter_core                 5.2.0
jupyter-events               0.6.3
jupyter_server               2.3.0
jupyter_server_terminals     0.4.4
jupyterlab-pygments          0.2.2
jupyterlab-widgets           3.0.5
keras                        2.11.0
kiwisolver                   1.4.4
libclang                     15.0.6.1
llvmlite                     0.39.1
Markdown                     3.4.1
MarkupSafe                   2.1.2
matplotlib                   3.7.0
matplotlib-inline            0.1.6
metakernel                   0.29.4
mistune                      2.0.5
nbclassic                    0.5.2
nbclient                     0.7.2
nbconvert                    7.2.9
nbformat                     5.7.3
nest-asyncio                 1.5.6
notebook                     6.5.2
notebook_shim                0.2.2
numba                        0.56.4
numpy                        1.24.2
nvidia-cublas-cu11           11.10.3.66
nvidia-cuda-nvrtc-cu11       11.7.99
nvidia-cuda-runtime-cu11     11.7.99
nvidia-cudnn-cu11            8.5.0.96
oauthlib                     3.2.2
opt-einsum                   3.3.0
packaging                    23.0
pandas                       1.5.3
pandocfilters                1.5.0
parso                        0.8.3
pexpect                      4.8.0
pickleshare                  0.7.5
Pillow                       9.4.0
pip                          22.0.2
platformdirs                 3.0.0
prometheus-client            0.16.0
prompt-toolkit               3.0.36
protobuf                     3.19.6
psutil                       5.9.4
ptyprocess                   0.7.0
pure-eval                    0.2.2
pyasn1                       0.4.8
pyasn1-modules               0.2.8
pycparser                    2.21
Pygments                     2.14.0
pyparsing                    3.0.9
pyrsistent                   0.19.3
python-dateutil              2.8.2
python-json-logger           2.0.6
pytz                         2022.7.1
PyYAML                       6.0
pyzmq                        25.0.0
qtconsole                    5.4.0
QtPy                         2.3.0
requests                     2.28.2
requests-oauthlib            1.3.1
rfc3339-validator            0.1.4
rfc3986-validator            0.1.1
rsa                          4.9
scikit-learn                 1.2.1
scipy                        1.10.0
Send2Trash                   1.8.0
setuptools                   67.6.0
six                          1.16.0
sniffio                      1.3.0
soupsieve                    2.4
stack-data                   0.6.2
tensorboard                  2.11.2
tensorboard-data-server      0.6.1
tensorboard-plugin-wit       1.8.1
tensorflow                   2.11.0
tensorflow-estimator         2.11.0
tensorflow-io-gcs-filesystem 0.30.0
termcolor                    2.2.0
terminado                    0.17.1
threadpoolctl                3.1.0
tinycss2                     1.2.1
torch                        1.13.1
torchaudio                   0.13.1
torchvision                  0.14.1
tornado                      6.2
traitlets                    5.9.0
typing_extensions            4.5.0
uri-template                 1.2.0
urllib3                      1.26.15
wcwidth                      0.2.6
webcolors                    1.12
webencodings                 0.5.1
websocket-client             1.5.1
Werkzeug                     2.2.3
wheel                        0.38.4
widgetsnbextension           4.0.5
wrapt                        1.14.1

and torch is a part of this list. I am confused as to why I cannot import the PyTorch package. Has anyone encountered this problem, and does anyone have a solution? If it’s helpful, I have also downloaded ROOT through the snap package manager on Ubuntu 22.04, which might be why I’m running into this problem.

Could you please post the output from running the following in bash?

snap run --shell root-framework
echo $PYTHONPATH
ls $PYTHONPATH
exit

I’m struggling to see an issue so far, but the snap doesn’t come with Torch preinstalled, so I’m wondering if this is perhaps from an older release that was designed for Python 3.8 and might need reinstalling now that the snap ships with Python 3.10. Unfortunately for PIP packages sideloaded outside the usual snap bundle, there’s not an amazing way to keep things in sync automatically; so if you’re keen on this approach you might like to run sudo snap refresh --hold root-framework to disable automatic updates and handle upgrades on your own terms.

You’d be able to rm -rf $HOME/snap/root-framework/current/.pip to clear up any pip packages that didn’t come as part of the bundle and then in theory you’d just have to reissue root-framework.pip install pytorch or similar to get them back; this time ideally with ABI compatibility for 3.10.

Thank you for your response. I get this as the output (which is likely concerning)

root@Ubuntu:/home/fellstinger# snap run --shell root-framework
2023/03/13 20:00:35.844348 cmd_run.go:1055: WARNING: cannot start document portal: read unix @->/run/user/1000/bus: EOF
mkdir: cannot create directory ‘/run/user/0’: Permission denied
root@Ubuntu:/home/fellstinger# echo $PYTHONPATH
/snap/root-framework/919/usr/local/lib:/root/snap/root-framework/common/.pip
root@Ubuntu:/home/fellstinger# ls $PYTHONPATH
ls: cannot access '/snap/root-framework/919/usr/local/lib:/root/snap/root-framework/common/.pip': No such file or directory

I will try your approach to see if it works.

Running snaps as the root-user doesn’t usually go well. Mandatory Access Control (i.e AppArmor in this case) will cause issues, such as with the document portal, and /run/user/0 above. The root-user wouldn’t shouldn’t even be able to access /home/fellstinger in a snap, so I’d also not be confident your work would be saved properly when run as root-user.

I’d heavily suggest operating outside a root-user shell where possible.

Since running things as the root-user can leave side effects, your best bet may be to purge the snap to clear any state, and reinstall it, with Torch if desired; like so

# All on a regular user terminal
sudo snap remove --purge root-framework
sudo snap install root-framework
root-framework.pip install pytorch
root --notebook

I’d then reasonably expect import torch to work in the Notebook without any issues, if that’s not the case; please let me know.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.