Can't install pytorch in online Jupyter Lab/Notebooks but can in Google Colab

For some reason I’m met with a slew of errors when trying to install pytorch in my online Jupyter Lab notebook environment, but the same commands work fine on Google colab which utilizes Jupyter notebooks, here are some of the commands I used in the Jupyter Lab notebook:

!pip install torch
!pip install torch torchvision
!pip install --upgrade pexpect (after restarting kernel and running in new first cell)

I kept getting this error: AttributeError: module ‘pexpect’ has no attribute ‘TIMEOUT’
Then in the console:

pip install torch
conda install pytorch
%pip install pytorch

With the magic command I got this error: ValueError: Can’t find a pure Python 3 wheel for ‘pytorch’

When I ran: !pip install torch torchvision in google colab, it worked fine. I’m just really confused why it would work on google colab which uses Jupyter notebooks, but it wouldn’t work in an actual Jupyter notebook. I’m pretty new to Python, Pytorch, and Jupyter so I apologize if I’m missing something basic. Any help is appreciated.

The main problem, I believe, is that you are trying to relate something running inside your web browser via web assembly (WASM) to typical, full Python running on a remote machine. JupyterLite and the WASM based kernel (pyodide-baed) is still very much in development and doesn’t have all the abilities a typical, full Python kernel does.

If you want a online Jupyter Notebooks/JupyterLab typical full Python kernel, go here and click on the ‘launch binder’ badge. Your session that will spin up will be a temporary remote session running on a remote linux machine. (Launches though have been a little rocky since Google stopped being a member of the Binder federation.) Make sure to save back to your local computer anything useful you make. You can switch from JupyterLab to the classic interface under the ‘Help’ menu along the top of the JupyterLab window.

The session you get from the process I describe above will be basic much like your Google Colab system. It won’t have Pytorch already installed. You can launch a session served via the MyBinder service with Pytorch already installed by going here and using the ‘launch binder’ badge way at the bottom.

Always share direct links in your posts.
And pay attention to the exact nature of the kernels indicated in the upper right corner.

I suspect your online Jupyter Notebooks are based on pyodide whereas the Google Colab is using a typical full, Python kernel running on a remote linux system. However, because you don’t provide links, I cannot for sure support this guess. The errors are indicative though.

And the issue is just as reported back to you when you attempt. Running %pip install pytorch in a cell in your notebook is telling you that it needs pure Python 3 wheel because web assembly cannot use stuff that isn’t pure Python at this time. The Google Colab instance can use Python integrated with C code.

Note: Be careful about some of your other false equivalencies. Google Colab uses a highly modified branch of an outdated version of Jupyter Notebooks.

This is why it doesn’t have the magic install commands that you should be using in modern Jupyter. Don’t use the exclamation point with install commands in modern Jupyter. Google Colab is the exception because in a lot of ways it is outdated and not modern.

Due to a lot of work by the developers, the same magic install commands for pip now work in both the typical, full Python kernels running in modern Jupyter or the Pyodide-based WASM kernels running in JupyterLite. If Google Colab wasn’t hanging on to using an outdated fork, it would be easy to state to always use the magic install commands in Jupyter everywhere without any caveat.

1 Like