Why I Prefer Python on WSL to Windows
Tags: wsl, python, todayilearned
Published: 2020-11-14 16:16:35

The only question is why? Even though it is only a three word question, it needs some explanations. I hate getting people to do things without telling, at least, a reason or two behind them. Most likely, in fact in this post, I just wanted to tell my stories using Python on WSL2.

I love [bleeding edge technology](https://en.wikipedia.org/wiki/Bleeding_edge_technology), so I have Python 3.9 installed by the time of writing. Of course, I have concerned that not all of my needs can be obtained easily. Notably, I did not get some wheel packages in [the PyPI](https://pypi.org/) available for this version 3.9. Despite knowing there are lots of great developers on the internet who provided [those wheels](https://www.lfd.uci.edu/~gohlke/pythonlibs/) unofficially, I just do not want to always rely on them. So, let us look at different scenarios.

Uh, wait, first we need to prepare the tools for building. Since by default, Fedora on WSL2 has brought these tools, we have nothing else to do. On Windows, we need several C++ development tools, i.e. `MSVC v142` and `Windows 10 SDK`, provided by [Visual Studio Installer](https://visualstudio.microsoft.com/downloads/) as shown below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/23s8nl6iy9ycmgyj3364.png)

For my sake, I have created a separated virtual environment only for this experiment on both Fedora and Windows system.

#\# 1

At the moment, the `scikit-learn` developer does not provide the wheel package for Python 3.9 yet, so we are going to build and install our own. The installation can be done simply by `pip install scikit-learn`. After a while, it has finished on both systems without having to worry at all. To confirm everything is okay, we just need to do a little checking by `python -c "import sklearn"`. Fedora did not show any output meaning it was running correctly, while Windows has made me feeling blue.

```powershell
> python -c "import sklearn"
 ** On entry to DGEBAL parameter number  3 had an illegal value
 ** On entry to DGEHRD  parameter number  2 had an illegal value
 ** On entry to DORGHR DORGQR parameter number  2 had an illegal value
 ** On entry to DHSEQR parameter number  4 had an illegal value
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\Naufan Rusyda Faikar\venv0\lib\site-packages\sklearn\__init__.py", line 80, in <module>
    from .base import clone
  File "C:\Users\Naufan Rusyda Faikar\venv0\lib\site-packages\sklearn\base.py", line 17, in <module>
    import numpy as np
  File "C:\Users\Naufan Rusyda Faikar\venv0\lib\site-packages\numpy\__init__.py", line 305, in <module>
    _win_os_check()
  File "C:\Users\Naufan Rusyda Faikar\venv0\lib\site-packages\numpy\__init__.py", line 302, in _win_os_check
    raise RuntimeError(msg.format(__file__)) from None
RuntimeError: The current Numpy installation ('C:\\Users\\Naufan Rusyda Faikar\\venv0\\lib\\site-packages\\numpy\\__init__.py') fails to pass a sanity check due to a bug in the windows runtime. See this issue for more information: https://tinyurl.com/y3dm3h86
```

By reading the message and going to [the given link]( https://tinyurl.com/y3dm3h86), I should realised that I only came to lament. To sit on my knees waiting for uncertainty. It is still being investigated! Maybe we will get a patch updates tomorrow morning, but consider the other things similar can happen after installing the patch. Is this meant by design? I wish no.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t262jw1kvo16p35idpn1.png)

#\# 2

When trying to install `PyAudio` for the first time without setting up anything beforehand, we are bound to get an error as shown below.

```powershell
> pip install PyAudio
...
src/_portaudiomodule.c(29): fatal error C1083: Cannot open include file: 'portaudio.h': No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.28.29333\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
...
```

Similar errors will be faced at Fedora as well. It appears that an attempt was made to find `portaudio.h` in the system. On Windows, we can build it manually using one of [the three options](http://portaudio.com/docs/v19-doxydocs/tutorial_start.html). But they are kind of painful tasks! So I was not willing to even intend to try it. Meanwhile, as expected, we only need to run a short command on Fedora to bring it home.

```bash
$ sudo dnf -y install portaudio-devel
Last metadata expiration check: 0:01:54 ago on Sat Nov 14 14:27:02 2020.
Dependencies resolved.
========================================================================================================================
 Package                          Architecture            Version                        Repository                Size
========================================================================================================================
Installing:
 portaudio-devel                  x86_64                  19-32.fc33                     fedora                   282 k
Installing dependencies:
 alsa-lib-devel                   x86_64                  1.2.4-5.fc33                   updates                  775 k
 portaudio                        x86_64                  19-32.fc33                     fedora                    95 k

Transaction Summary
========================================================================================================================
Install  3 Packages

Total download size: 1.1 M
Installed size: 17 M
...

$ pip install PyAudio
Collecting PyAudio
  Using cached PyAudio-0.2.11.tar.gz (37 kB)
Building wheels for collected packages: PyAudio
  Building wheel for PyAudio (setup.py) ... done
  Created wheel for PyAudio: filename=PyAudio-0.2.11-cp39-cp39-linux_x86_64.whl size=51785 sha256=eacf6885675e75fa6ab23a4c592c35b22a2caff88f0c894dc516d821f26c5419
  Stored in directory: /home/naru/.cache/pip/wheels/76/e7/d6/193c174cc4cba9409e8eecea8f9e986fc9c88e08160759dfe8
Successfully built PyAudio
Installing collected packages: PyAudio
Successfully installed PyAudio-0.2.11
```

Is that going to work? Yes, at least it can be imported without any error. But no, for WSL does not support audio, although there is [a way of](https://x410.dev/cookbook/wsl/enabling-sound-in-wsl-ubuntu-let-it-sing/) making it work.

Anyway, you got the idea! I sincerely hope that [`winget`](https://github.com/microsoft/winget-cli) will take on the same level of benefits of GNU/Linux distribution package manager!

#\# 3

I am very much to know that this will not be a fair comparison, but I will let you decide. On Windows, we got:

```python
> ipython
Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: class Hello:
   ...:     def __init__(self):
   ...:         ...
   ...:     @staticmethod
   ...:     def greeting():
   ...:         ...
   ...:

In [2]: %timeit Hello()
141 ns ± 1.54 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [3]: %timeit Hello.greeting()
82.7 ns ± 0.527 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [4]: import sys

In [5]: sys.getsizeof(Hello())
Out[5]: 56

In [6]: sys.getsizeof(Hello.greeting())
Out[6]: 16
```

While on Fedora, we got:

```python
$ ipython
Python 3.8.6 (default, Sep 25 2020, 00:00:00)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: class Hello:
   ...:     def __init__(self):
   ...:         ...
   ...:     @staticmethod
   ...:     def greeting():
   ...:         ...
   ...:

In [2]: %timeit Hello()
126 ns ± 0.996 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [3]: %timeit Hello.greeting()
78.3 ns ± 0.546 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [4]: import sys

In [5]: sys.getsizeof(Hello())
Out[5]: 48

In [6]: sys.getsizeof(Hello.greeting())
Out[6]: 16
```

Why does something on Windows turn out to be so expensive for simple things?

---

I have a lot to share, but that is all for now. Shhh, by the way, [Guido van Rossum](https://en.wikipedia.org/wiki/Guido_van_Rossum) has decided [to join Microsoft!](https://twitter.com/gvanrossum/status/1326932991566700549) Will Python be able to do any better on Windows?