Originally published on my blogging site ajeet.dev
This blog post is a step-by-step guide to install and use Python for web development, programming, data science or machine learning on a Windows machine using the Windows Subsystem for Linux and VS Code Editor. This post is written exclusively for the beginners in tech.
I have planned to write several blog posts on Python. All of them will be focused on the beginners to get them started with Python for Data Science. Please follow the updates here : Learn Python Step by Step - A Blog Series by Me š
I am a newbie in tech. This blog post is based on my personal experience. Feedback Welcome. Would really appreciate your time š
Techies haveĀ differentĀ set-ups on a Windows machine to use Python for web development, machine learning or data science. For the newbies in tech, I recommendĀ any one of the following two development environment set-ups (also recommended by Microsoft) to start their coding journey:
- Use Windows Subsystem for Linux (WSL) and VS Code: This is the approach we are going to follow in this blog post.Ā We will use a Microsoft tool - Windows Subsystem for Linux - to set up a Linux distribution on a Windows machine.Ā Newbies in tech who have been using a Windows desktop for years must read this blog post explaining the benefits of using Windows Subsystem for Linux. In this set up, weĀ will install and useĀ Python inside the WSL, not directly on Windows. I am using this setup for two reasons: One, BuildĀ web applications using Python - Most of the resources and instructions for Python web development are written by and for the Linux users. The resourcesĀ use Linux-based packaging and installation tools. Thus, the support is endless for the Linux users.Ā ReadWhy Linux Is Better For Programming & Web Dev. Second, LearnĀ data science and machine learning - I can extend this set up to learn data science or machine learning. For example, I can install Jupyter andĀ natively edit Jupyter notebooks in VS Code inside the WSL. I canĀ easily install and use several Python's data science & machine learning libraries to do several tasks such as data cleaning, analysis, visualizations, and build machine learning models.
- Use WindowsĀ 10 and VS Code: You can use this set up to use Python for something other than web development. In this case, we installĀ Python directly on Windows 10 using the Microsoft Store. This will help you learn Python using Windows, but not using the WSL tool. At any point of time, you can always install WSL to learn web development. Learn more about this setup here. I don't use this setup to learn programming using Python. Read my viewsĀ on why I chose a Linux environment to learn programming.Ā
Table of Contents
- š» Windows Subsystem for Linux, VS Code Editor, Python Extension = The Deadly Trio
- ā Ā Install Windows Subsystem for Linux (WSL)
- āļø Set up VS Code to work with WSL
- š ļøĀ Install Python and its modules inside WSL
- ā¬ļø Install Python Extension to work with VS Code
- šāāļøĀ Run a Python program
- š” What's Next - Setup Jupyter Notebook with VS Code
- š§° Additional resources for continued learning
š» Windows Subsystem for Linux, VS Code Editor, Python Extension = The Deadly Trio
Microsoft has been working really hard to develop various new-age tools for the developers and techies to bring web development, data science and machine learning to the masses.Ā It created Azure cloud computing service and TypeScript open-source programming language. It acquired GitHub andĀ R software. It developedĀ source-code editor VS Code, Windows Subsystem for Linux (a tool to use Linux on windows without installing any virtual machine software), and has been continuously working to improve Microsoft Terminal. It clearly shows Microsoft tools have been slowly gaining the popularity among data scientists, engineers, and developers.
This combination of Windows Subsystem for Linux (WSL), VS Code editor and Python extension is truly a solid set up to work with Python for anything, be it data science, machine learning, artificial intelligence or web development.
We will install and use the three tools by Microsoft to learn Python on Windows 10. Let us start installing one by one.
ā Ā Install Windows Subsystem for Linux (WSL)
I have already written a how-to-guide to set up WIndows Subsystem for Linux (WSL). FollowĀ this guideĀ to set up WSLĀ - a powerful tool built by the Microsoft. In this guide, you will learn how to install and use Linux inside a Windows machine without installing any Virtual Machine software. You don't have to leave your favorite Windows UI anymore. Additionally, use this guide (optional) to create files and folders inside WSL.
āļø Set up VS Code to work with WSL
For the beginners, I have written a comprehensive guide on how to work with VS Code editor inside the WSL. FollowĀ this step-by-step guide.Ā In this guide, you will learn how to set up VS Code as our text editor / integrated development environment (IDE) to work with WSL. The cool thing is you can run all the Linux commands, such as mkdir
to create a project directory and touch
to create a file, using VS Code integrated terminal.
So, you don't have to leave you favorite Code Editor too.
š ļøĀ Install Python and its modules inside WSL
If you have followed my WSL guide, you would have seen Ubuntu terminal by now. UbuntuĀ 18.04 LTS comes with Python3 version installed. But it leaves out some of the modules and packagesĀ that Python applications often usesĀ that donāt come as part of the standard library. We will install aĀ couple of them.
Before installing them, one should make sure Python3 is already installed. To confirm, run python --version
in the VS Code integrated terminal. You will see Python version (version 3.6.9 in my case). Make sure you have WSL installed on your Windows machine, and you have set up VS code server using the guides in the previous section. To update the Python version, first you should update your Ubuntu version using this command:Ā sudo apt update && sudo apt upgrade
. The terminal will ask you for the password you set up during installing the WSL tool. Type your password (the password will not be visible) and pressĀ EnterĀ button using your keyboard. During this process, if the terminal asks you "Do You want to continue? [Y/n]", just press Y using your keyboard, and hit Enter button. ItĀ is basically seeking your permissionĀ to download something from the internet. Make sure you read first.
Now, run this command to update Python version: sudo apt upgrade python3
Next, we will install pip - the standard package manager for Python. RunĀ this command in the same VS Code integrated terminal:sudo apt install python3-pip
. Pip is a package installer for Python that helps us install packages from the Python Package Index. You can install Pandas - a Python package used for data analysis, time series, and statistics by running this command: pip install pandas
. Note that depending on your environment, you might have to use pip3 instead of pipĀ in the above command.
Now, we will install venv - a module that provides support to create and manage lightweight virtual environments. Run this command in the terminal:sudo apt install python3-venv
.
Learn more about pip and venv.
ā¬ļø Install Python Extension to work with VS Code
Once you have installed the Python packages and modules inside Remote - WSL, you must install the VS CodeĀ Python extension inside WSL even if you have installed it locally (Windows machine). If you don't understand how extensions work locally and inside Remote - WSL, you must read this short info on managing VS extensions.
To install Python extension, press CNTRL+SHIFT+X button simultaneously using your keyboard. This will bring up the Extensions view in the Left/Right side of the Code Editor. In the top Search box, enter "Python". Look for "Python (ms-Python.Python)" by Microsoft extension. Click on Install or Install in WSL:Ubutnu-18.04 button.
Once you have installed the extension, VS Code will ask you to reload the
code editor. Click on Reload Required button, if asked. This will reload VS Code. The final view may look like this:
Python is an interpreted language, which means we should tell our text editor, here VS Code, to select anĀ interpreter in order to run Python code using VS Code editor. Python supports different types of interpreters (Python2, Anaconda, etc). We will use Python 3.6.9 though you can install different versions of Python and use them as interpreters for different projects. To choose a Python 3 interpreter, bring up theĀ Command PaletteĀ view of the VS Code. PressĀ Ctrl+Shift+PĀ button simultaneously using your keyboard, typeĀ Python: Select Interpreter, and selectĀ it from the drop-down.
Once you click the Python interpreter,Ā the drop-down will show you Python
versions installed inside WSL. I can see only one as I have installed one only version of Python. Click on "Python 3.6.9 64-bit".
Microsoft will download the Python language server. Once the download finished,
you would see the Python version you selected as an interpreter in the Bottom Status bar.
If you don't see the version you like to choose in the dropdown, follow this guide toĀ configure Python environments.Ā
Let us test our set up if it actually works. Open VS Code integrated terminal. If you don't know how integrated terminal works, please go through my blog post on how to set up VS Code to work with WSL.Ā EnterĀ the command: python3 . You will get the Python interpreter.Ā Run the command: print("I am Ajeet")
. You will see the statement "I am Ajeet".
Newbies should try out some more simple codes in the Python interpreter. Stop the terminal by pressingĀ Ctrl+ZĀ buttonĀ keyboard buttons in the terminal. You will see the text "Stopped ... Python3". Toclear/delete the commands you entered in the terminal, pressĀ Ā Ctrl+LĀ button.Ā This completes our Python set up process inside the Remote - WSL.
šāāļøĀ Run a Python program
We just ran a simple code in Python interpreter. Let's create a simple Python program and run it using VS Code inside WSL. This will also make sureĀ we have selected the correct Python interpreter. Follow these steps:
In the VS Code integrated terminal, enter command ls
to see all the current project directories. Create a new project directory "ajeet_python" by entering this command: mkdir hello_python
. Create a Python file inside this folder. Enter touch ajeet_python/learn.py
. The .py means we are creating a Python file. Now go to the newly created folder. Enter cd ajeet_python
. To see the newly created Python file, enter ls
. You can also see both the file and folder in the left side bar of the VS Code UI (called VS Code File Explorer).
Inside the newly create Python file learn.py, let us write a code and save it. Open the file by double clicking on it.Ā Copy this statementĀ print("Hello to everyone")
paste it in the file, and save by pressingĀ Ctrl+ZĀ button keyboardĀ buttons. Now, we have to run the Python "Hello to everyone" program. In the integrated terminal, enter python3 learn.py
Ā . This will run your program you just created, and the Python interpreter will printĀ "Hello to everyone" in the terminal window.
Yay šš Congrats! We have set up our Python development environment on Windows machine.
š” What's Next - Setup Jupyter Notebook with VS Code
I am going to write about setting up the Jupyter notebook with VS Code editor to learn data science and machine learning on Windows 10. Blog post coming tomorrow at my blogging website. Meanwhile, here is the Table of Contents.
š§° Additional resources for continued learning
IĀ recommend the following resources to help you in continuing to learn Python on Windows using WSL and VS Code. Don't miss!
- Learn Python Step by Step - A Blog Series by Me š
- Create a virtual environment
- Create a simple app using Flask - a Python web framework
- Create a simple app using Django - a Python web framework
- All the latest updates about Python at Microsoft
- Developing with Python on Windows
- Microsoft Guides to learn Python
Credits š :Ā
Official Microsoft Docs on WSL
Emojis taken fromĀ Emojipedia
Cover image built with BlogCover
Top comments (6)
Great read! I also find useful using flake8 plugin for VSCode together with
wemake-python-styleguide
:wemake-services / wemake-python-styleguide
The strictest and most opinionated python linter ever!
wemake-python-styleguide
Welcome to the strictest and most opinionated python linter ever.
wemake-python-styleguide
is actually a flake8 plugin with some other plugins as dependencies.Quickstart
You will also need to create a
setup.cfg
file with the configuration.We highly recommend to also use:
Running
This app is still just good old
flake8
And it won't change your existing workflow.See "Usage" section in the docs for examples and integrations.
We also support GitHub Actions as first class-citizens Try it out!
What we are about
The ultimate goal of this project is to make all people write exactly the same
python
code.Hi Nikita,
Thanks for the suggestion. I have just started learning programming in Python.l, a beginner in web dev as well.. I will try this out tomorrow. Thank you for your contribution to this post š
I've found that powershell + python + vscode = The Real Deadly Trio (on windows of course).
Powershell is mature and enough powerfull (despite the name...), while WSL is still immature (read: slow). I write my code both on windows and linux and I switch from powershell to bash back and forth without thinking about it.
Hi Nicola,
Thank you for your comment. Agree with you that they are really good on a Windows machine. I wanted to make sure newbies get comfortable with a few tools first. Then they can extend their toolset. Microsoft Terminal Preview is also evolving. I did give it a try but for now sticking with Vs Code with its Python extension as I have to deal with only one UI. Of course i can switch back and forth with Powershell/terminal.
Regarding WSL, i can say this tool is evolving. The improvement from WSL1 to WSL2 is great.
For the beginners who don't want to leave Windows UI but learn programming using a Linux distro, i recommend this. Of course you cannot play heavy games inside the WSL for now. Hopefully it will get better soon.
Thanks for your time š
The goal of my comment is exactly for this: reduce the tools. Vscode (+ Python extension)and python are enough! Powershell is default and vscode is set to use it from the beginning. Itās already a lot for a newbie, so WSL can came in later when (and IF) itās needed (especially because the benefits are minimal).
I mentioned this in first paragraph of the post. For web dev, most of the devs either use a Linux distro or MacOS. So, WSL is recommend to learn the basics.
Thank you for your feedback :)