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. Read Why Linux Is Better For Programming & Web Dev. Two, 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.
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.
O, 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
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 dropdown.
Once you click the Python interpreter, the dropdown 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”. To clear/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.
$ touch ajeet_python/learn.py
The .py means we are creating a Python file. Now go to the newly created folder.
$ 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")
and 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.