Tag Archives: Python

Python ‘pip’ and Windows registry corruption


In my recent post about setting up a 64 bit Windows Django development environment, I described an initial problem with Virtualenv which then led me through a whole series of problems and fixes and finally to what I thought was a working 64 bit Python development environment. However, I recently realised that the original underlying issue still remained, such that any attempt to install a new package using pip would result in failure due to that same error message.

For example, when I tried to install Beautiful Soup via pip, I got the following error:

C:\> pip install beautifulsoup4
Downloading/unpacking beautifulsoup4
Cleaning up...
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\pip\basecommand.py", line 122, in main
status = self.run(options, args)
File "C:\Python27\lib\site-packages\pip\commands\install.py", line 278, in run

requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundl
File "C:\Python27\lib\site-packages\pip\req.py", line 1229, in prepare_files
File "C:\Python27\lib\site-packages\pip\req.py", line 292, in run_egg_info
logger.notify('Running setup.py (path:%s) egg_info for package %s' % (self.s
etup_py, self.name))
File "C:\Python27\lib\site-packages\pip\req.py", line 265, in setup_py
import setuptools
File "C:\Python27\lib\site-packages\setuptools\__init__.py", line 12, in
from setuptools.extension import Extension
File "C:\Python27\lib\site-packages\setuptools\extension.py", line 7, in
from setuptools.dist import _get_unpatched
File "C:\Python27\lib\site-packages\setuptools\dist.py", line 15, in
from setuptools.compat import numeric_types, basestring
File "C:\Python27\lib\site-packages\setuptools\compat.py", line 19, in
from SimpleHTTPServer import SimpleHTTPRequestHandler
File "C:\Python27\lib\SimpleHTTPServer.py", line 27, in
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
File "C:\Python27\lib\SimpleHTTPServer.py", line 208, in SimpleHTTPRequestHand
mimetypes.init() # try to read system mime.types
File "C:\Python27\lib\mimetypes.py", line 361, in init
File "C:\Python27\lib\mimetypes.py", line 263, in read_windows_registry
with _winreg.OpenKey(hkcr, subkeyname) as subkey:
TypeError: must be string without null bytes or None, not str

Continue reading Python ‘pip’ and Windows registry corruption

Create a Django development environment on 64-bit Windows for Heroku deployment

Getting Started with Django on Heroku

This weekend I tried to follow Heroku’s Getting Started with Django article. Unfortunately for me, this article is clearly aimed at developers working in a “unix-style environment”, and I develop with a 64-bit Windows machine both at work and at home. Given that we are working with a Python-based framework, I foolishly assumed that it would be trivial to translate these instructions to Windows. How wrong I was.

This article summarises all the issues I encountered and how I eventually worked around them. I found most of the individual answers I needed via Google, but I couldn’t find a single resource that explains this whole process. I hope it saves you some time. The specific error-messages and console output were generated with 64-bit Windows 8.1, but I expect things to be basically the same with 64-bit Windows 7.

Continue reading Create a Django development environment on 64-bit Windows for Heroku deployment

Unlock your TrueCrypt Encrypted Device without the Exact Password

So here’s the rather embarrassing story…

A couple of years ago, I encrypted a USB stick using TrueCrypt encryption to store some important/valuable files. I then put it in my bag and forgot about it. This weekend I came back to it and realised I had forgotten the password. I could remember using a combination of a couple of other passwords for increased security, but I couldn’t remember which passwords I had used, or in what order or combination. After numerous manual attempts to find the right combination, I gave up resigned to the fact that I would just have to wipe the device and start again.

Then something occurred to me – “Hang on a minute, I’m a programmer!”. And then I thought “How could I lose such an important password?”. Since I encrypted this USB stick, I’ve become a disciplined user of KeePass and LastPass, and to be fair this is the only password not in my password database. But still, it must be possible for me to solve this problem.

The first piece of the puzzle was the command-line interface to the Windows version of TrueCrypt. This is more than adequate to be wrapped with a script. The second piece of the puzzle was the itertools Python module, which provides some very nice functions for iterating over various permutations/combinations of values.

So the stage was set – how easy will it be to write a Python script to solve this problem? It turns out the answer is pretty easy. I found the itertools.product() function to be the best fit for my requirements, as it will generate the Cartesian product of an iterable list of possible password components with itself, giving me every possible combination of the specified password components.

The other critical part of my solution was discovering the right combination of command-line arguments to provide TrueCrypt. Here is the magic Python statement, which will attempt to mount the specified device as drive letter “T:” using the given password in a non-interactive manner:

truecrypt_command = "\"%s\" /q /s /v %s /lT /m ro /a /p \"%s\" /b" % ( truecrypt_exe, truecrypt_device, password )

The rest was just glue code to read in the list of possible password components from a separate text file, and then loop over every possible combination/permutation until the password is found (or we run out of options).

Good news – the script works, and it found my missing password! The script is very basic and still uses some hard-coded settings, but you can download TrueCryptPasswordHunter.py if you think it will be of any use to you. It was written and tested with Python v2.7.3 32 bit on a Windows 7 64 bit machine.


Create a text file called password_components.txt in the same directory as the script, and populate it with possible password components, one per line. For example, if you think your missing password might be “monkeydoghorse” or “horsedogmonkey”, or something similar, then your password components file should contain the following:


Then just run the script and cross your fingers.