Hi there, I'm Savannah. Today I'm going to talk to you about using Windows
Subsystem for Linux with Python and Visual Studio Code. Before jumping
into the VS Code or Python specific components of this demo I should
probably start by describing what the windows subsystem for Linux is.
The windows subsystem for Linux or WSL is effectively a Linux environment on
your Windows machine. This comes without the overhead of virtual machine and
allows you to run most command line tools, utilities, and applications
directly on your Windows machine just like you would on your Linux operating
system. So I'm not going to walk you through the installation steps for WSL
today, however I will link to some helpful resources at the end of this
video and I will point out that you can install your favorite distribution
directly from the Microsoft Store on Windows. I've chosen to install Ubuntu
18.04 which is my favorite distribution but we have many distributions that you
can choose from depending on your preference. Once you've done your basic
WSL installation as well as this sold your preferred distribution for WSL
you'll want to open VS Code and navigate to the extensions and search for WSL and
install the Remote - WSL extension to enable Windows Subsystem for Linux and
VS Code. After we've installed WSL and our preferred distribution we can open a
command prompt and see that we are in fact still in Windows. However it's super
simple to switch over to a Linux environment using the command prompt by
simply typing WSL and we'll notice that after that runs we'll see that our
route has changed and we are now in Linux. We can actually confirm that we're
in Linux by typing LSB_release-a and we'll see that we are in
fact in Ubuntu 18.04 which is my preferred distribution and this will
vary depending on which distribution you've installed. So if we want to
move over and open our VS Code project we could just simply use our Linux
commands to navigate to the appropriate directory. I store all my stuff in
code and then we can navigate inside the
directory and then simply type code .
So once the project opens in VS Code we can notice WSL and a couple different
places. First because I've opened this project before both in a Windows
environment as well as an Ubuntu environment I can see the same
application my useful website flask appear twice. This is because they were
used in two different environments. I can also take notice that
they have different paths because they exist in different file systems. If I go
to the bottom left corner I can also see that I'm in a remote connection which is
WSL Ubunto. If I click that I'm able to navigate between different environments.
This is a super easy way to toggle between Windows environment and WSL
environment and I'm also able to close my remote connection should I want to. So
now we're all set to start using our project in our WSL environment in VS
Code. So we'll start by opening a new terminal and we can see that we're still
in Linux so we're still able to use our Linux commands. So I'll start by creating
a virtual environment. Once that finishes up we can activate the virtual
environment and we'll see now that we are in our virtual environment. So now I
can go ahead and install my requirements for the project and we can see that it's
installing flask and then it will install pi test. So after that we'll
have all of our dependencies installed and we'll be ready to run our app.pi
file. So now that that's done we can open our app.pi file and we can notice
that the Python extension is loading in VS Code. Once that's done we're all ready to
run this file. So next we'll hit play on my app.pi
file, and we'll see that the development server is starting up, the
flask app is running. If I navigate back to the browser and refresh we see that
the web app is loaded. So what's especially cool about this is that I'm
running the application in my WSL environment. My browser exists in Windows.
So one more thing that I want to show you is that you're also able to still
debug your code even though you're using a WSL environment within VS Code
just like you would in windows. So if I go back to VS Code and I stop the web
application, I can navigate to the debugger using this triangle and bug
icon. I've already configured my launch.JSON
file to set up my debugger and I can see that I've set a breakpoint on
line 15 which is sys.platform. If I click run then I can go back to my
browser and click refresh so that I hit the endpoint in Flask, and I can see that
VS Code is notifying me that something interesting is happening. I can go to
the line which I put the breakpoint on and I see that it is highlighted and now
again we can see that the platform that this code is being run on is Linux which
is especially interesting because it reinforces once again that our browser
is in Windows but our coding environment is in Linux. And that's pretty much it.
So to recap what we went over today we talked about what WSL is, how to install
it alongside your favorite distribution, how you can convert a command prompt to
a Linux terminal, how you can open a project in VS Code using WSL, and we
showed that we can still continue to debug and run our code in WSL
environment within VS Code. If you want to try out what I demoed today, you can
start by visiting the following links. Thanks for watching and feel free to
reach out and let us know how it goes.