How to link WSL to my already installed python on windows?

So, recently I installed the WSL from the Windows Store and now I'm having some problems with python. I already have python and some of its libraries installed on my Windows 10, but whenever I type py in the Ubuntu terminal it doesn't understand it, it only understands when I type "python3". Is there a way to add the py command to activate python? Also in the interactive mode ($python3 -i), whenever I try to import any library (i.e numpy) it throws an error (ModuleNotFoundError: No module named 'numpy') while I already have numpy installed on my Windows. It's like the WSL is acting like it can't even see python and its libraries installed in my windows!

an image of my WSL struggling


Solution 1:

As explained in Windows Subsystem for Linux interoperability with Windows, there are some conditions that need to be satisfied in order to execute Windows programs from the WSL command line:

Run Windows tools from WSL

WSL can invoke Windows binaries directly from the WSL command line using [binary name].exe. For example, notepad.exe. To make Windows executables easier to run, Windows path is included in the Linux $PATH in Fall Creators Update.

Some irrelevant stuff omitted

Windows binaries must include the file extension, match the file case, and be executable. Non-executables including batch scripts. CMD native commands like dir can be run with cmd.exe /C command.

So, if your Windows version of python3 is a regular executable, you should be able to run it as

python3.exe

Otherwise (apparently this includes the version of python3 installed to WindowsApps from the Microsoft store) you will need to use

cmd.exe /C python3.exe

Solution 2:

So here's the problem. WSL has a different version of python installed, as opposed to your windows machine. You can link them like this:

Run these commands in WSL:

$ sudo apt remove python3 python3-pip
$ sudo apt autoremove

Now in your .bashrc on WSL, add the following:

alias python3="cmd.exe /C python3"
alias pip3="cmd.exe /C pip3"

Now restart your WSL application, and there! It should work! This worked for me very well...