Score:0

jupyter notebook can use numpy but not ubuntu in wsl2

fr flag

The problem is when I try to use numpy or any other module from the ubuntu terminal in WSL@. It throws an error, but it works seamlessly from jupyter-notebook.

Also, WSL2 ubuntu doesn't load the notebook. It seems like a path issue but not been able to pinpoint it.

Thanks in advance.

enter image description here

enter image description here

Saha_1994 avatar
fr flag
@steeldriver I manually installed it from the Microsoft store. So, I was surprised.
Saha_1994 avatar
fr flag
@steeldriver Thanks for pointing out the mistake. I updated it.
Score:1
us flag

numpy is not a part of the Python Standard Library, and isn't installed by default on most Linux distros, including WSL, which is an Ubuntu terminal environment. For this reason, you can't just import it without first installing the package.

There are two ways that you can install additional Python packages to WSL. The first is to install directly from Ubuntu repositories:

$ sudo apt install python3-numpy
$ python3
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> 

The repositories won't contain every single Python package, only those that have been packaged for Ubuntu. These will all be very stable, however.

The second way is to use the pip package manager, which must itself be first installed from the repositories:

$ sudo apt install python3-pip
$ pip install numpy
$ python3
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> 

pip installs packages from the Python Package Index, which contains almost every Python package under the sun. Many of these are just in development, don't work, or are abandoned, but all the mainstream packages are there too.

It doesn't matter much which method you choose, but it is tidy to pick one, and stick with it for all your Python packages. Nothing will stop you from installing the same package from both sources, which can be different versions.

The reason that this works without issue in jupyter is probably because it is running inside an environment, probably setup with anaconda, which already contains these packages.

Saha_1994 avatar
fr flag
But is there a way to use the already installed software that is available to be used by the Jupyter? Before this new distro of Ubuntu20.04 that I used in WSL2, I had a previous version of Ubuntu and it worked fine.
aswheaton avatar
us flag
Depends on how you installed Jupyter to begin with. It doesn't usually come installed by default either. Did you do it via conda? If you're unsure, you can look in the log file `~/.bash_history`
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.