Can not import torch_xla on google colab without tpu

username: xinli
cooked: <p>Hi,<br>
I want to use torch_xla to verify some problems on Google Colab. Due to the lack of TPU resources, I am using a CPU environment. The default environment for Colab is Python 3.9, but the <a href="https://colab.research.google.com/github/pytorch/xla/blob/master/contrib/colab/getting-started.ipynb" rel="noopener nofollow ugc">official tutorial</a> for torch_xla uses 3.8. Therefore, I switch Python to 3.8 and installed torch and torch-xla according to the tutorial. I can see the torch-xla module when I use the ‘pip3 list’ command. However, I encountered an error when importing torch_xla: <strong>ModuleNotFoundError: No module named ‘torch_xla’</strong><br>
I would like to know if torch_xla must run in a TPU environment. If not, where did I go wrong? Thanks a lot.<br>
Here is my Google Colab ipynb file</p>
<details>
<summary>
command and output</summary>
<p><strong>!sudo apt-get install python3.8 python3.8-dev python3.8-distutils python3.8-venv</strong></p>
<p>Reading package lists… Done<br>
Building dependency tree<br>
Reading state information… Done<br>
Note, selecting ‘python3-distutils’ instead of ‘python3.8-distutils’<br>
python3-distutils is already the newest version (3.8.10-0ubuntu1~20.04).<br>
python3-distutils set to manually installed.<br>
python3.8 is already the newest version (3.8.10-0ubuntu1~20.04.6).<br>
python3.8 set to manually installed.<br>
python3.8-dev is already the newest version (3.8.10-0ubuntu1~20.04.6).<br>
python3.8-dev set to manually installed.<br>
The following additional packages will be installed:<br>
python-pip-whl<br>
The following NEW packages will be installed:<br>
python-pip-whl python3.8-venv<br>
0 upgraded, 2 newly installed, 0 to remove and 22 not upgraded.<br>
Need to get 1,810 kB of archives.<br>
After this operation, 2,339 kB of additional disk space will be used.<br>
Get:1  amd64 python-pip-whl all 20.0.2-5ubuntu1.8 [1,805 kB]<br>
Get:2 amd64 python3.8-venv amd64 3.8.10-0ubuntu1~20.04.6 [5,448 B]<br>
Fetched 1,810 kB in 1s (2,609 kB/s)<br>
debconf: unable to initialize frontend: Dialog<br>
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76, &lt;&gt; line 2.)<br>
debconf: falling back to frontend: Readline<br>
debconf: unable to initialize frontend: Readline<br>
debconf: (This frontend requires a controlling tty.)<br>
debconf: falling back to frontend: Teletype<br>
dpkg-preconfigure: unable to re-open stdin:<br>
Selecting previously unselected package python-pip-whl.<br>
(Reading database … 128275 files and directories currently installed.)<br>
Preparing to unpack …/python-pip-whl_20.0.2-5ubuntu1.8_all.deb …<br>
Unpacking python-pip-whl (20.0.2-5ubuntu1.8) …<br>
Selecting previously unselected package python3.8-venv.<br>
Preparing to unpack …/python3.8-venv_3.8.10-0ubuntu1~20.04.6_amd64.deb …<br>
Unpacking python3.8-venv (3.8.10-0ubuntu1~20.04.6) …<br>
Setting up python-pip-whl (20.0.2-5ubuntu1.8) …<br>
Setting up python3.8-venv (3.8.10-0ubuntu1~20.04.6) …</p>
<p><strong>!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1</strong></p>
<p><strong>!sudo update-alternatives --config python3</strong></p>
<p>There are 2 choices for the alternative python3 (providing /usr/bin/python3).</p>
<h2>
<a name="selection-path-priority-status-1" class="anchor" href="#selection-path-priority-status-1"></a>Selection    Path                Priority   Status</h2>
<ul>
<li>0            /usr/bin/python3.9   2         auto mode<br>
1            /usr/bin/python3.8   1         manual mode<br>
2            /usr/bin/python3.9   2         manual mode</li>
</ul>
<p>Press  to keep the current choice[*], or type selection number: 1<br>
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python3 (python3) in manual mode</p>
<p><strong>!python3 --version</strong></p>
<p>Python 3.8.10</p>
<p><strong>!sudo apt install python3-pip</strong></p>
<p>Reading package lists… Done<br>
Building dependency tree<br>
Reading state information… Done<br>
The following additional packages will be installed:<br>
python3-setuptools python3-wheel<br>
Suggested packages:<br>
python-setuptools-doc<br>
The following NEW packages will be installed:<br>
python3-pip python3-setuptools python3-wheel<br>
0 upgraded, 3 newly installed, 0 to remove and 22 not upgraded.<br>
Need to get 584 kB of archives.<br>
After this operation, 2,621 kB of additional disk space will be used.<br>
Get:1  amd64 python3-setuptools all 45.2.0-1ubuntu0.1 [330 kB]<br>
Get:2  amd64 python3-wheel all 0.34.2-1ubuntu0.1 [23.9 kB]<br>
Get:3  amd64 python3-pip all 20.0.2-5ubuntu1.8 [231 kB]<br>
Fetched 584 kB in 1s (1,022 kB/s)<br>
debconf: unable to initialize frontend: Dialog<br>
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76, &lt;&gt; line 3.)<br>
debconf: falling back to frontend: Readline<br>
debconf: unable to initialize frontend: Readline<br>
debconf: (This frontend requires a controlling tty.)<br>
debconf: falling back to frontend: Teletype<br>
dpkg-preconfigure: unable to re-open stdin:<br>
Selecting previously unselected package python3-setuptools.<br>
(Reading database … 128312 files and directories currently installed.)<br>
Preparing to unpack …/python3-setuptools_45.2.0-1ubuntu0.1_all.deb …<br>
Unpacking python3-setuptools (45.2.0-1ubuntu0.1) …<br>
Selecting previously unselected package python3-wheel.<br>
Preparing to unpack …/python3-wheel_0.34.2-1ubuntu0.1_all.deb …<br>
Unpacking python3-wheel (0.34.2-1ubuntu0.1) …<br>
Selecting previously unselected package python3-pip.<br>
Preparing to unpack …/python3-pip_20.0.2-5ubuntu1.8_all.deb …<br>
Unpacking python3-pip (20.0.2-5ubuntu1.8) …<br>
Setting up python3-setuptools (45.2.0-1ubuntu0.1) …<br>
Setting up python3-wheel (0.34.2-1ubuntu0.1) …<br>
Setting up python3-pip (20.0.2-5ubuntu1.8) …<br>
Processing triggers for man-db (2.9.1-1) …</p>
<p><strong>!pip3 install cloud-tpu-client==0.10 torch==1.13.0 <a href="https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-1.13-cp38-cp38-linux_x86_64.whl" rel="noopener nofollow ugc">https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-1.13-cp38-cp38-linux_x86_64.whl</a></strong></p>
<p>Looking in indexes: ://pypi.org/simple, ://us-python.pkg.dev/colab-wheels/public/simple/<br>
Collecting cloud-tpu-client==0.10<br>
Using cached cloud_tpu_client-0.10-py3-none-any.whl (7.4 kB)<br>
Collecting torch==1.13.0<br>
Using cached torch-1.13.0-cp38-cp38-manylinux1_x86_64.whl (890.2 MB)<br>
Collecting torch-xla==1.13<br>
Using cached ://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-1.13-cp38-cp38-linux_x86_64.whl (151.3 MB)<br>
Collecting google-api-python-client==1.8.0<br>
Using cached google_api_python_client-1.8.0-py3-none-any.whl (57 kB)<br>
Collecting oauth2client<br>
Using cached oauth2client-4.1.3-py2.py3-none-any.whl (98 kB)<br>
Collecting typing-extensions<br>
Downloading typing_extensions-4.5.0-py3-none-any.whl (27 kB)<br>
Collecting nvidia-cudnn-cu11==8.5.0.96<br>
Downloading nvidia_cudnn_cu11-8.5.0.96-2-py3-none-manylinux1_x86_64.whl (557.1 MB)<br>
anylinux1_x86_64.whl (21.0 MB)<br>
anylinux1_x86_64.whl (317.1 MB)<br>
e-cu11==11.7.99<br>
Downloading nvidia_cuda_runtime_cu11-11.7.99-py3-none-manylinux1_x86_64.whl (849 kB)<br>
plate&lt;4dev,&gt;=3.0.0<br>
Downloading uritemplate-3.0.1-py2.py3-none-any.whl (15 kB)<br>
Collecting google-auth-httplib2&gt;=0.0.3<br>
Downloading google_auth_httplib2-0.1.0-py2.py3-none-any.whl (9.3 kB)<br>
Collecting google-auth&gt;=1.4.1<br>
Downloading google_auth-2.16.2-py2.py3-none-any.whl (177 kB)<br>
odules&gt;=0.0.5<br>
Downloading pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)<br>
ent already satisfied: wheel in /usr/lib/python3/dist-packages (from nvidia-cublas-cu11==11.10.3.66-&gt;torch==1.13.0) (0.34.2)<br>
Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from nvidia-cublas-cu11==11.10.3.66-&gt;torch==1.13.0) (45.2.0)<br>
Collecting protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,&lt;4.0.0dev,&gt;=3.19.5<br>
Downloading protobuf-3.20.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.0 MB)<br>
mon-protos&lt;2.0dev,&gt;=1.56.2<br>
Downloading googleapis_common_protos-1.58.0-py2.py3-none-any.whl (223 kB)<br>
alizer&lt;4,&gt;=2<br>
Downloading charset_normalizer-3.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (195 kB)<br>
odules, google-auth, idna, charset-normalizer, urllib3, certifi, requests, googleapis-common-protos, google-api-core, pyparsing, httplib2, uritemplate, google-auth-httplib2, google-api-python-client, oauth2client, cloud-tpu-client, typing-extensions, nvidia-cublas-cu11, nvidia-cudnn-cu11, nvidia-cuda-nvrtc-cu11, nvidia-cuda-runtime-cu11, torch, absl-py, torch-xla<br>
Successfully installed absl-py-1.4.0 cachetools-5.3.0 certifi-2022.12.7 charset-normalizer-3.1.0 cloud-tpu-client-0.10 google-api-core-1.34.0 google-api-python-client-1.8.0 google-auth-2.16.2 google-auth-httplib2-0.1.0 googleapis-common-protos-1.58.0 httplib2-0.21.0 idna-3.4 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 oauth2client-4.1.3 protobuf-3.20.3 pyasn1-0.4.8 pyasn1-modules-0.2.8 pyparsing-3.0.9 requests-2.28.2 rsa-4.9 six-1.16.0 torch-1.13.0 torch-xla-1.13 typing-extensions-4.5.0 uritemplate-3.0.1 urllib3-1.26.15</p>
<p>{“pip_warning”:{“packages”:[“certifi”,“google”]}}</p>
<p><strong>import torch</strong></p>
<p><strong>!pip3 list</strong></p>
<p>Package                  Version</p>
<hr>
<p>absl-py                  1.4.0<br>
cachetools               5.3.0<br>
certifi                  2022.12.7<br>
charset-normalizer       3.1.0<br>
cloud-tpu-client         0.10<br>
dbus-python              1.2.16<br>
google-api-core          1.34.0<br>
google-api-python-client 1.8.0<br>
google-auth              2.16.2<br>
google-auth-httplib2     0.1.0<br>
googleapis-common-protos 1.58.0<br>
httplib2                 0.21.0<br>
idna                     3.4<br>
nvidia-cublas-cu11       11.10.3.66<br>
nvidia-cuda-nvrtc-cu11   11.7.99<br>
nvidia-cuda-runtime-cu11 11.7.99<br>
nvidia-cudnn-cu11        8.5.0.96<br>
oauth2client             4.1.3<br>
pip                      20.0.2<br>
protobuf                 3.20.3<br>
pyasn1                   0.4.8<br>
pyasn1-modules           0.2.8<br>
PyGObject                3.36.0<br>
pyparsing                3.0.9<br>
python-apt               2.0.1<br>
requests                 2.28.2<br>
requests-unixsocket      0.2.0<br>
rsa                      4.9<br>
screen-resolution-extra  0.0.0<br>
setuptools               45.2.0<br>
six                      1.16.0<br>
torch                    1.13.0<br>
torch-xla                1.13<br>
typing-extensions        4.5.0<br>
uritemplate              3.0.1<br>
urllib3                  1.26.15<br>
wheel                    0.34.2<br>
xkit                     0.0.0</p>
<p><strong>import torch_xla</strong></p>
<hr>
<p>ModuleNotFoundError                       Traceback (most recent call last)<br>
 in <br>
----&gt; 1 import torch_xla</p>
<p>ModuleNotFoundError: No module named ‘torch_xla’</p>
<hr>
<p>NOTE: If your import is failing due to a missing package, you can<br>
manually install dependencies using either !pip or !apt.</p>
<h2>
<a name="to-view-examples-of-installing-some-common-dependencies-click-the-open-examples-button-below-2" class="anchor" href="#to-view-examples-of-installing-some-common-dependencies-click-the-open-examples-button-below-2"></a>To view examples of installing some common dependencies, click the<br>
“Open Examples” button below.</h2>
</details>

username: JackCaoG
cooked: <p>Hi, sorry for the late reply. XLA can be run with CPU, GPU and other accelerators. I suspect what you encountered is a pip issue. We shipped the python 3.9 whl for the 2.0 release. For the colab you can use <code>https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-2.0-cp39-cp39-linux_x86_64.whl</code>. A complete list of whls is under <a href="https://github.com/pytorch/xla#wheel" class="inline-onebox" rel="noopener nofollow ugc">GitHub - pytorch/xla: Enabling PyTorch on Google TPU</a>.</p>
<p>Btw we check github issue more often, feel free to open a github issue under <a href="https://github.com/pytorch/xla/issues" class="inline-onebox" rel="noopener nofollow ugc">Issues · pytorch/xla · GitHub</a> if you run into any additional issues.</p>
<p>BTW, we check</p>