Setup python on Linux in Windows using WSL and VS Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends welcome to team python Series in this video we're going to talk about how to set up python on Linux in Windows using WSL and vs code supports for WSL and python if you don't know what WSL is WSL means windows subsystem for Linux and it's a feature in Windows using which you can run Linux environments inside windows without any dual boot or virtual machines I've already made a video and blog post on setting up WSL in Windows I will leave the link of that video in the description so you can check it out to know how to set up WSL in Windows so these are the topics we're going to discuss now first let's try to set up python pip and VM in the WSL Ubuntu and then let's see how we can install multiple python versions using dead snakes PPA and then we're going to talk about how to run python programs on installing python packages in WSL and how to set up virtual environments in Python projects inside WSL Ubuntu and we will take a look at how vs code can make working with WSL easy and productive and then we're going to take a look at update Alternatives command in Ubuntu so that we can create aliases for python versions so let's try to set up python pip and VM in Ubuntu first let's try to open a command prompt let us see whether I have any WSL distro installed so I'm gonna write WSL minus L minus V and you can see I have no WSL distributions installed so let's try to install and Ubuntu WSL distribution so let's try to see what are my options I'm gonna write wsn minus L minus o which means list out the online distributions so I can install Ubuntu 2204 so let's try to install that I'm going to write WSL minus minus install Ubuntu 2204 and now Ubuntu 2204 will be installed so that's it we have installed Ubuntu 22204 so this is a brand new Ubuntu 22.04 Linux distribution installed in WSL all right now let's talk about setting up python python comes pre-installed in Ubuntu so if you want to open this distribution again let's try to open that again I'm going to close this Ubuntu terminal and I'm going to open the normal command prompt and let's try to write WSL so if you write WSL your session will be started with Ubuntu distribution so now I'm in the Ubuntu machine so let's try to see whether python is installed or not so I can just write Python 3 minus minus version so I've got python already installed it's 3.10.6 and then I can see where the location of python is so I can write which Python 3 so the Python 3 is installed in user bin Python 3 folder and then if you want to update your Python 3 distribution you can just write sudo apt upgrade python 3. so it's already upgraded but if your Python 3 is not upgraded it can be easily upgraded using this command sudo at update python3 and that's it our python is set up now now let's try to set up pip because using pip you can actually install external python packages so installing it is really easy you can just write sudo apt install Python 3 pip so let's try to write that command sudo app install Python 3 pip I am getting this message because I did not update my packages so let's try to do that now I'm gonna write sudo apt update and the night sudo app upgrade and let's say yes for everything and now our packages are updated let's try to run the command again sudo app install Python 3 pip let's just say yes so Python 3 pip is installed so we can use the PIP module to install external python packages and then we can also install VM for creating virtual environments in our python projects inside Ubuntu so installing VM is very simple you can just write sudo apt install python 3vnp so I'm gonna write sudo apt install python3vn so that's all python 3vn is also installed so we can create virtual environments in our python projects all right the next thing we want to talk about is how to set up multiple python versions inside Ubuntu so there is this cool repository called dead snakes PPA and using this repository you can install multiple python versions so first you have to add the repository of XNX PPA so you should run this command sudo add app repository and then PPA dead snake PPS PPA so let's try to write that sudo add app repository PPA colon dead snake slash PPA so that's next PPA is installed now I can install multiple python versions so let's try to install python 3.7 so I can write sudo apt install python 3.7 so python 3.7 is installed so I can just see python 3.7 version minus minus version and you can see Python 3.7.16 and you can even run python 3.7 you can just write python 3.7 you are running Python 3.7.16 and if you just write Python 3 you're running python 3.10.6 but there is one more command you need to install which is python 3.7 disk utils because if you want to run pip commands you have to use this distributive so let's try to copy this and write sudo apt install python 3.7 Dash distributes so now using this python 3.7 I can even do the PIP install so let's try to see that now python 3.7 minus M which is module of fifth so let's try to enter and here you can see the pick command is working this is because we have actually installed python 3.7 Dash distributes also on it that's how you can set up multiple python versions using the dead snakes PPA in this example I also installed python 3.9 so let's try to do that now in our example also let's try to copy these commands and paste it here let's try to just write and and let's just say yes for everything all right we also installed python 3.9 you can see which python 3.9 and it's already installed in user bin python 3.9 all right enough of installation let's try to run python in WSL so I'm going to take a blank folder and I'm going to create a new file let's try to write it Hello dot pi and let's try to edit it let's write print hello world I'm saving it and closing it and let's try to open a command prompt here so I'm going to write CMD and I got my command prompted in the folder so if you want to run the python script using the python installed in the WSL Linux distribution it's really simple you don't need to even open the Linux distribution just write WSL behind the command so I'm going to write WSL python3 Hello dot pi and that's it without opening the WSL shell you are able to run the WSL command by just writing WSL in front of it I'm telling inside the WSL run the command python3 Hello dot Pi so it's that simple to run Python scripts in Linux using WSL so now if I want to run python with the python 3.7 which I've installed I can just write WSL python 3.7 hello.5 and that's it we have run hello.pi with another python version which is python 3.7 so that's how you can run Python scripts inside the WSL Linux environments all right let's try to run a flask server inside this WSL Linux environment but for that you need to install flask using pip right so let's try to do that now I'm going to open the WSL shell so now our WSL shell is active and let's try to write the command Python 3 minus M pip install flask so I'm installing flash module inside the Python 3 installation of the Linux environment all right flask is installed in the WSL Linux environment now you can just check Python 3 minus M tip list and there's a model called flask installed all right let's try to write a server.pi file here so I'm going to open this folder in vs code now I'm going to right click and open with vs code so let's try to create a file and just name it server.pi so let's write a very simple flask server our flash server is ready let's try to run this using the python installed inside the WSL Linux environment so I'm going to go back to the command line and if you say LS you got our server.pi so let's try to run this server.pi so I'm going to write python3 server.pi and you can see the server is listening at localhost 1500 so let's try to open that in our browser localhost 1500 and here you can see Hello World so the best part is even if the flash server is running inside the Linux environment you can directly access it inside the windows environment and WSL takes care of it automatically and that's how simple you can run a flash server in WSL even if you exit the WSL and if you are inside the folder and you can just write without opening the WSL WSL python3 server.pi so without even open the WSL shell you can run the python server inside the WSL Linux environment all right we've opened vs code let's try to see how vs code can support WSL so let's try to close this command prompt now and let's try to run this server from vs code but if I run the server now it will run from Windows so there is a bit setup to be done in vs code to open a folder from WSL Linux distribution so first I'm going to search for extensions so I will write WSL so WSL is an extension that should be installed first I've already installed WSL vs code extension and then after installing the WSL vs code extension you can just click on this WSL connection icon so click on this and then you can see a reopen the folder in WSL currently the folder is open using Windows vs code but now if you reopen the folder in WSL you can see starting vs code in WSL Ubuntu so now the vs code instance is running in the Ubuntu Linux distribution running in WSL so if I just open this Hello dot pi and try to run I don't see any python play button because python extension for WSL is still not installed so let's just go to the extensions and then write Python and here you can see python is installed in my windows but it's not installed in WSL Ubuntu so let's try to install that in Ubuntu also so now after installation you can see extension is enabled on WSL Ubuntu 22204 so now if you open this Hello dot Pi you can see the button here and if you run this it's asking to select a python interpreter so I already have some three types of python interpreters which is 3.10 3.9 and 3.7 which I've installed now so let's try to select the default python installation inside user bin Python 3 and now let's try to run it again and here you can see without any commands we are able to run the python script inside WSL and the best part is you can very easily select python watch also in vs code just by clicking on this python version and let's try to run it with 3.7 now user pin python 3.7 just click on it and run it and now using 3.7 you're actually running your python script all right let's get back to python 3.10.6 let's set random server also now so let's try to run it and you can see in vs code very easily we are able to run flash server in WSL Ubuntu so let's try to reload this page again and you can see it's working the best part is you can even debug your python script inside WSL Ubuntu using this vs code WSL extension so let's try to run it in debug mode and now let's go to the browser and click enter and here our code is being debugged so it's pretty convenient to even debug python files using vs codes WSL extensions this is how the WSL extension and vs code actually makes working with Linux environments inside WSL very easy and productive all right let's try to see an example of virtual environment also in this WSL python environment so I'm going to open a command prompt here and by default it has opened the session inside the wsle Linux environment so let's try to create a virtual environment here so I'm going to write Python 3 minus mvn which is a VN module and let's try to write the name of personal environment as app underscore EnV and now a virtual environment is created you can see the folder app underscore n you can activate the virtual environment from command prompt very easily you can just write source and the path of that virtual environments activate script so it's going to be dot slash app underscore n bin and activate so you can see that here app and score and Bin and activate so you have to run the script so let's run this and here you can see our app and virtual environment is activated but you need to write this command in command prompt if you're not using vs code if you are using vs code you can just click on this python version and select the virtual environments python so here I got a recommendation app VM so let's try to click that and now I've got my virtual environment selected so if you don't get this recommendation just click on enter The Interpreter broth find it and enter this you got bin python so let's try to run it in the virtual environment and obviously we get there are no model flash because it's completely another environment and now in this environment you can just write Python 3 minus MP install flask and Flash will be installed so now flask is installed in this virtual environment and then you can run flash with this virtual environment now this is how you can set up and manage virtual environments inside WSL python projects all right let's talk about update Alternatives command for aliasing python versions to a keyword called python so if you just write a command like this let's try to copy this and let's try to open a command prompt and open WSL in it so I opened a command prompt and let's try to open WSL session and let's say to paste this so I'm writing update Alternatives install user bin python so there is actually no python there's only Python 3 in this WSL so I'm telling install a new package inside this user bin python name it Python and it should be aliased from this python 3.9 so if I just click enter obviously I need permission so I'm gonna run it as a sudo and now if I say which python I got user bin python but if I run python it's python 3.9 and if you want to remove the aliases just write update Alternatives minus remove all python then the python Alias will be removed from the PlayStation Ubuntu system the reason why I mentioned update Alternatives command is because you can use a single Alias called Python and just change the underlying python version by using update Alternatives command so that your shell script will have the keyword python but you can change the python version in the background using the update Alternatives command so that's it guys this is how you can set up python in Linux inside windows WSL Ubuntu distribution you can see I've created a blog post on this topic and I've also given you the commands and examples so that you can copy paste and practice it in your own computer so be sure to check out the link of this blog post in the description of this video please ask questions or post your valuable feedback in the comments section hope you like this video guys thank you for watching peace
Info
Channel: Learning Software
Views: 14,423
Rating: undefined out of 5
Keywords: tutorial, taming_python, taming_python_skill, wsl, windows, vm, ubuntu, setup, vscode, python
Id: hK-fZhh4v8I
Channel Id: undefined
Length: 14min 36sec (876 seconds)
Published: Tue Feb 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.