Andrew Fontaine

Mostly code

Development on Windows - Python

16 Aug 2015

Oh, boy. Python! This is part 3 of 3 on our “Development on Windows” series, and by “part 3 of 3,” I of course mean I haven’t thought about more Windows development issues… yet. If I can come up with more, I will be adding more posts. Tweet at me if you think of any issues you have. Maybe I’ll tackle git one day.

There’s a couple reasons why setting up Python on Windows is the hardest. First, a couple of tools that come highly recommended for Python aren’t compatible on a plain Windows install. Second, I’ve never been able to successfully install a C-based extension in Windows. I’m trying a new approach that might be successful as I write this, so hopefully it works out okay.

UPDATE: It didn’t.

So. Python. Python is used a lot in scientific settings, I’ve been told. It’s handy and easy to learn, so if someone wanted to learn how to program, Python is usually considered a good place to start. A lot of the time, these people who are willing to learn have Windows computers, and are told the easiest way to start are to use fancy online courses. These people, are, of course, totally right, but these tools only get us so far. What if we want to build something real on our computers? Not many people want to drop even more money on a Mac or struggle to learn how to use Linux. It’s gotta be Windows. Let’s get started.

Download and Install

As usual, the first thing to do is to download Python. You can get various installers from their downloads page. If you don’t know which version to download, get the latest web-based installer of your CPU architecture. It’s probably x86-64, unless your computer is super-old. Run the installer through to the end, and be sure to add python to your PATH. It’s one of the installer options.

As of the last time I installed Python, which is version 3.4, it doesn’t add everything you need to your PATH, so follow the guide in my Ruby post, and add .PY to your PATHEXT and %PYTHONINTSALL%/scripts to your PATH. That directory is where pip and easy_install are installed, and also where any executables from pip packages are installed. Once all that’s done, open a ConEmu window, and try running python and pip to make sure we’re rolling. If that hasn’t worked, try giving your computer a reboot. It’s Windows, that usually does the trick.

Virtualenv and Virtualenvwrapper

Fortunately, Virtualenv works with Windows without an issue. just pip install virtualenv, and we’re on our way. Virtualenvwrapper, on the hand, is a different story. I like Virtualenvwrapper, so of course there had to be a Windows solution out there.

Lucky for us, there are two: a powershell and a command prompt variant. We’ll go over the one I got working (on Windows 10, even).

Virtualenvwrapper-powershell

Clearly the one we want, yes? Perhaps not. As of this writing, it hasn’t been updated since 2014 (albeit to make it compatible with Powershell v4). More importantly: its install script isn’t compatible with Python 3. We shouldn’t need Python installed to install the Powershell module. All we have to do is drop the VirtualEnvWrapper directory in our Powershell modules directory, which, by default, is %USERPROFILE%\Documents\WindowsPowerShell\Modules\ for users. We should then be able to add the line Import-Module VirtualEnvWrapper to our $PROFILE, restart Powershell and be on our way!

Of course, it’s never really that easy.

Installation

  1. Be sure to change your execution policy to RemoteSigned

    $ Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
    
  2. Drop the module files in, as described above.
  3. Unblock all the files so they execute.

    $ GetChild-Item -Recurse %USERPROFILE%/Documents/WindowsPowerShell/Modules/VirtualEnvWrapper | Unblock-File
    
  4. Add the import module line to your $PROFILE
  5. Edit line 112 of the file Win.psm1 to the following:

    $pyVer = & { $OFS = ""; "$((& "python.exe" "-V" 2>&1).split(" ")[1][0..2])" }
    
  6. Add keys in the registry:

    $ regedit
    

    Under HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore, create a new key that is the python version (in this case, 3.4). Under that, add two more keys, InstallPath, with a value containing your python install path (probably C:\Python34) and PythonPath, with the value C:\Python34\Lib;C:\Python34\DLLs;C:\Python34\Lib\lib-tk

With that all said and done, we should have a working install! As always, make sure to make any appropriate customizations (for instance, my install is under D;\Python34), and you’ll be all set. Look over the virtualenvwrapper docs for a command reference, and python to your heart’s content.

NOTE: By default, the Virtualenvwrapper puts all your virtualenvs in %USERPROFILE%\.virtualenvs, but you can change this by defining the environment variable WORKON_HOME. We can customize all sorts of other things, but this is sufficient for now.

C Extensions

A lot of python libraries are written in C for speed reasons. As usual, there is no good way to build these. The ruby language has the dev-kit, and we can take advantage of it, but it doesn’t work in all cases. To set this up, we need to make a file in %USERPROFILE%\pydistutils.cfg containing the following:

[build]
compiler = mingw32

Before running pip install, we also need to add the dev kit to our path. We can do this by running %DEVKITPATH%\devkitvars, and that’ll add the MinGW toolchain to our %PATH%. If this still doesn’t work, generally the error messages will contain a list of c libraries you’ll need to download.

If you don’t want to bother with any of this, a tonne of unofficial Windows builds for these libraries are available here. Just activate your virtualenv, download the whl file, and run

$ pip install file.whl

I wish there was a better way to do this, but unfortunately I don’t know if it.

After all this lovely work is done, we have a suitable python development environment. Make a new virtual environment for your project, and get to work!

Happy pythoning!

I ❤ feedback. Let me know what you think of this article on Twitter @afontaine_ca