I truly enjoy the journey of learning python and being able to play with the API accessibility of some of the systems I’ve built and designed. Along the way, I became accustomed to many examples running from within Linux, as my development environment. For that reason I had been using, Windows subsystem for Linux, WSL, to run linux environment side by side with Windows 10. Although my laptop is indeed a MacBook Pro, my virtual desktop (VMware Horizon Desktop) is windows 10. Having WSL became quite the convenience. I also became accustomed to using visual studio code due to its extensibility. So finding that I could use VS code to work and interact directly with my WSL instance was fantastic. Here are some quick steps to get WSL running in Windows 10 and to connect it to VS code. As a note I already assumed that you have Visual Studio code installed on your Windows machine. If not it’s an easy install and you can click the following link to get that now.
VISUAL STUDIO CODE
Open Control panel, and on the left-hand side select “turn Windows features on and off”. From there scroll down and select “Windows subsystem for Linux” next hit “ok”
After hitting ok, your Windows device will then need to restart.
After your Windows machine has restarted open the Microsoft store.
From there go ahead and search for “WSL” to search for Windows subsystem for Linux. In this case I’m selecting Ubuntu 20.04
Go ahead and hit install. Depending on your machine and Internet connection the time this step will take may vary.
Next hit launch to enter the Windows subsystem. It will then bring up a commandline prompt
You will be asked to enter a username and password to secure WSL
Now that you are at the standard prompt my next step would be to update and upgrade any packages that have already been installed on the system
Once that completes go ahead and double check the version of Python that is preinstalled is up-to-date by typing “python3 -V” in this case for me I had Python version 3.8.2
My next step will be to install Python package manager PIP as well as venv for virtual environments “sudo apt install python3-pip python3-venv”
Here I am simply double checking my working directory
My next step would be to create a folder to store all of my Python environments neatly by doing a “sudo mkdir NAMEOFFOLDER” after creating your new folder go ahead and change ownership to yourself recursively with the following space “sudo chown -R plasebikan /home/plasebikan/virt-envs” replacing my username with your own as well as changing the path to the folder you had created in the previous step
Next go ahead and change directories to the folder you just created. What I did next as an example was to create two Python environments. You can create a new Python environment with python3 -m venv NAME-OF-ENV as shown in the example above, where I create sample environment one and sample environment to
While you’re in this folder one thing of note is that you can open Visual Studio code from WSL. For this walk-through I would do so. When you type “ code .” The space followed by the period is intentional. It will open Visual Studio code with that folder that you are currently in, in this case it is my Python environment folder. When you do this Visual Studio will install extension binaries into your WSL instance.
As you can see Visual Studio code has opened your folder in the left-hand pane explorer. We can also see our two virtual environments.
For this example I will right-click your top level folder or anywhere in that space and Select new file. In the picture above I create A new Python file named mytest.py. You may see Visual Studio code pop up with a window asking you if you want to install the recommended Python extension, into your WSL instance, I would do so at this time.
If you take a look at your bottom left-hand corner from within Visual Studio code you will see that is connected to WSL as well as which version of the interpreter for Python is being used. In this case you can see we have sample environment one active. If you want to select another environment simply click on the current interpreter in use to get a drop-down of your available interpreters on system. In this case since we have two virtual environments available to us you can select sample environment to in this case. From there all you have to do is open a new terminal window to make use of that new environment. (Open new teminal window with
When you go to extensions in the left-hand pane this is where you will see the extension that was installed into your WSL instance, it will be titled remote – WSL or something close to that if the name does end up changing.