Python and Windows get cozier

Microsoft makes it easy to install and use Python in Windows and in Visual Studio Code

Python and Windows get cozier
Thinkstock

If you ever complained that Windows was the only major operating system not to ship with Python, it’s time to stop complaining. It may not be in the installer, but it’s now an official part of Windows, as an optional feature. The third most popular language (according to analyst firm RedMonk’s regular programming language survey) has finally become a first-class citizen.

Microsoft is using Python to experiment with a new way of installing and deploying Windows features, using the Microsoft Store as a host. In the past you had to go to the Store, search for an app or package, and then install it. This new method is a lot simpler. Getting Python into Windows is now only a matter of typing python at a Windows command line and installing the Windows Store Python package. Instead of being purely a way to install apps, Microsoft is treating the Store as a package manager, much like the familiar tools in many Linux distributions, including those running on WSL (Windows Subsystem for Linux).

Installing Python from the command line

The new installation method rolled out with the 1904 release of Windows 10 in May 2019. The Store package for Python is based on the community distribution of Python 3.7, optimized for interactive and educational use. If you want an alternative distribution, you can install it via the standard Windows Python installer, or with bundled installers such as Anaconda, or via Visual Studio’s data science and machine learning tools. Microsoft worked with the Python release team to develop a Store package, providing new tools in Windows to launch the Store when a user types python on a machine that’s not been configured with the language.

If you’ve already installed Python and added it to your Windows PATH, then the new installation feature won’t run, and the python command will launch your existing Python environment. This will ensure that the Microsoft Store release can’t overwrite the settings.

Using the Store isn’t recommended for production work at present, as the package is regularly updated through the Microsoft Store and changes can affect interactions with Python libraries and tools. You’re limited to working inside the security context that Microsoft uses for Store apps, restricting access to those protected by Windows’ internal sandboxing. However, it’s a good way to get familiar with a popular and important language, with the Windows command line working as a Read Eval Print Loop for interactive code development, and with access to Python’s own IDLE combined REPL/IDE.

Simplifying installation in this way makes it a lot easier for developers to share Python code with Windows users, especially if you’re developing cloud-native applications and want to test them on Windows development PCs. Microsoft has been putting a lot of effort into making Windows a developer-friendly platform again, and the combination of WSL and a Windows Python release goes a long way to providing a much-needed foundation for integrating familiar macOS and Linux toolchains with Windows.

[ Also on InfoWorld: 24 Python libraries for every Python developer ]

Python in Visual Studio Code

Installing Python is only part of the story. Microsoft has made great effort to provide support for it in Visual Studio Code, with an extension that quickly turns its cross-platform developer’s editor into a Python IDE. Installed from the Code Extensions Marketplace, it’s integrated with your local Python install and works on MacOS and Linux as well as on Windows (and with WSL via VS Code’s remote editing feature). It’s not the only Python tool in the marketplace; many developers have shared their own tool, and Anaconda bundles VS Code with its own Python distribution.

Once the Python extension and your choice of Python interpreter have been installed, you can access it from VS Code’s built-in terminal. Microsoft recommends launching VS Code from inside your development directory, as this sets up a project-specific workspace with its own settings. You can then use the command tools to select the Python environment you intend to use. Selecting an interpreter sets the path VS Code uses for your project; you can set a default Python environment by choosing an interpreter without defining a workspace, so you will always have Python tooling wherever you open files, as it will automatically detect the .py extension as a Python program.

The Python extension for VS Code uses VS Code’s language server to give you syntactically colored code and uses it with IntelliSense to automatically provide code completion and linting. If you’ve installed Python modules into your Python installation, they’ll be covered by IntelliSense too, adding their own autocompletions. It’ll handle adding appropriate methods to variables once they’ve been assigned types.

VS Code’s terminal will work as a basic REPL for your code, and you can use it as a scratchpad for code as well as a tool for testing and debugging applications. One useful option is the ability to select a group of lines in your code and run them in the terminal so you can isolate and try out a specific code block without running an entire application.

Debugging and using Jupyter Notebooks

One useful feature of VS Code’s Python extension is its debugger. This supports local and remote debugging, working with ptvsd on remote systems. The local debugger supports breakpoints, with tools for inspecting local variables. If you’re in the debug console you can use it to set values or quickly evaluate new methods against your code, allowing you to try out new options without having to change your files. The debugger can help spot uninstalled packages and provides a way to set up and manage virtual Python environments where you can install and test packages without affecting other code.

Microsoft regularly updates its Python extension, adding new features and additional support. The most recent update, November 2019, will now automatically use VS Code’s language server to import missing packages as required, even identifying popular abbreviations to ensure the correct packages are installed. Other recent additions include support for plot visualizations inside VS Code, using its interactive Python Window and its native support for Jupyter Notebooks.

That last new feature is important for anyone doing data science with Python and Visual Studio Code, taking advantage of its many statistical and numerical methods packages. It gives you a notebook-like editing environment for your code, creating blank notebooks and adding support for Markdown text formatting as well as for Python code and in-line visualizations. It will work with both local and remote Jupyter servers and can take a notebook and turn it into running code once you’ve finished experimenting.

Supporting Python across Windows and across its development tools is a sensible move for Microsoft. Python’s popularity shows no signs of waning, and it’s a key tool for building machine learning algorithms, an important market for Azure. A quick install option gets it into the hands of more developers, and tools in Visual Studio Code give access to a low-cost development and test environment, without needing investment in the full Visual Studio IDE. Now that getting started is this easy, maybe learning Python should be on your to-do list for 2020.

Copyright © 2019 IDG Communications, Inc.