Python and Windows

About Me

Python on Windows is popular

The 2 faces of Python on Windows (pre 2011)

CPython - A native port of Python on Windows.

IronPython - An implementation of Python using the .NET environment

The 2 faces of Python on Windows (post 2011)

CPython - A native port of Python on Windows.

IronPython - An implementation of Python using the .NET environment

I'm not dead yet. It's only a flesh-wound
It's only a flesh wound!

The 2 faces of Python on Windows (post 2011)

CPython - A native port of Python on Windows.

CPython + WinRT??

It's not at all clear how this is going to pan out.

A minor digression

Where is Windows heading?

.NET was to be the future

.NET was to be safe

.NET was to be safe

Windows 8/Metro

Watch this space!

Mark's free opinion
- you get what you pay for!

Mark's free opinion
... until someone tells me to stop!

b2g and chromium are difference shades of the same thing. b2g is designed to run on a different class of hardware and with different hardware accessories, but otherwise the same idea - the web is the app!

Python on Windows

CPython on Windows

CPython on Windows

Much of CPython uses unicode windows apis

Python for Windows Extensions (pywin32)

Expose Windows specific features

Broad coverage
native IO, services, networking, COM, UI, etc

stable and mature

Python for Windows Extensions (pywin32)

Leveraged by many other projects;
things like twisted, mercurial etc all take advantage of pywin32 if found.

Downside: fairly large, not easy_install-able

Cross-platform?

Python has rich cross-platform support - so why target Windows specific features?

So if Python is cross-platform, why do I care about Windows specific features?

For administrators

For users

Integrate with development tools

Windows Management Instrumentation (WMI)

Windows Management Instrumentation (WMI)

But can't all these be done without WMI?

Yes, but only with various low-level API calls.

Which is the fallback if WMI can't do it...

WMI example

Show all automatic services which are not running

import wmi
c = wmi.WMI()
stopped = c.Win32_Service(StartMode="Auto", 
                          State="Stopped")
for s in stopped:
    print s.Caption, "service is not running"
    

Non WMI version

    from win32service import (
        OpenSCManager, EnumServicesStatus, QueryServiceConfig, 
        OpenService, CloseServiceHandle, 
        SC_MANAGER_ENUMERATE_SERVICE, SERVICE_WIN32, SERVICE_INACTIVE, 
        SERVICE_ALL_ACCESS, SERVICE_AUTO_START)
    hscm = OpenSCManager(None, None, SC_MANAGER_ENUMERATE_SERVICE)
    for s in EnumServicesStatus(hscm, SERVICE_WIN32, SERVICE_INACTIVE):
        hs = OpenService(hscm, s[0], SERVICE_ALL_ACCESS)
        c = QueryServiceConfig(hs)
        if c[1] == SERVICE_AUTO_START:
            print name, "is not running"
        CloseServiceHandle(hs)
        
evil
Don't try and understand it, but instead marvel at the amount of domain knowledge needed to even start.

WMI availability

Packaging Python apps - py2exe

What's new with Python on Windows?

Python Launcher

Python launcher

PEP 397

'Shebang' processing for Windows

Useful from the command-line too.

Installed with Python 3.3 and available separately

Python launcher - why now?

Previously 'last version installed wins'

Python 3.x made this a real problem.

Launcher Shebang processing

Compatible with *nix shebang lines.
#! /usr/bin/python
#! /usr/bin/env python

Windows specific shebang lines.
#! c:\python27\python.exe
Minimal shebang lines
#! python3

Version specific shebang lines

Symlinks are often used on Linux to specify a version.
#! /usr/bin/python3

Launcher handles this too
#! /usr/bin/python3
#! /usr/bin/python2.7

Launcher on the command-line

c:\> py script.py
Launches the latest installed Python 2.x - No need for them all to be on the PATH

c:\> py -3.2 script.py
c:\> py -3.3-32 script.py
Launch 3.2, 32bit 3.3

The 64bit version of Python is always preferred if both are found. -32 allows you to override this.

Full Unicode Implementation in 3.x

All use of Windows API by core Python uses Unicode

Implementation detail which is completely invisible, but still nice!

Optionally add Python to the PATH

Links

os.kill

os.kill does something useful on Windows.

Slightly different semantics and usage than *nix.

RTFM

VirtualEnv

VirtualEnv is a tool to create isolated Python environments.

Now built into Python itself.

Includes support for Windows

Makes an existing useful tool even more useful!

Using new MSVC

3.3 uses Microsoft Visual C++ 2010

End to manifest hell

No visible change.

Extension authors must upgrade compiler

Summary

Questions?