5

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

2 Answers 2

10

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
5
  • 1
    Also keep in mind that Windows python is going to behave differently than WSL python, so you might just want to install Python3 within WSL.
    – Thomas Ward
    Commented Jul 24, 2019 at 13:59
  • so if i installed python 3 using WSL, will there be a conflict with the already installed python? I mean if i installed it, it would be like installing python in a whole different machine, and hence i will install the libraries. right? Commented Jul 24, 2019 at 14:41
  • @YoussefHeshamAlsoly as far as I know that's correct - however that's really up to Microsoft Commented Jul 24, 2019 at 15:03
  • thanks a lot, I will try it ASAP and hopefully won't mess up anything Commented Jul 24, 2019 at 15:20
  • A snip is an obscure term for anyone not being a native English speaker, so edited and upvoted! ;-) Drop by in chat sometime... 0:-)
    – Fabby
    Commented Jul 27, 2019 at 17:31
1

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...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .