The bitarray Python module

It is fairly common in many applications to read or write series of bits into bytes or longer array of bytes. For example, to store results of various binary tests. The space saved by storing these results as bits might be crucial when memory or disk resources are the bottleneck.

If you deal with an array of bits, the Python module you need is bitarray.

Installing it for Python 2.x is simple:

$ sudo apt install python-bitarray

If you are using Python 3.x:

$ sudo apt install python3-bitarray

Creating an empty bitarray is easy:

import bitarray
ba = bitarray.bitarray()

Note that the endianess of the data is very important in these applications. bitarray uses big-endian interpretation by default. If your data is stored in little-endian, then specify this explicitly:

ba = bitarray.bitarray(endian="little")

You can check the endianess of any bitarray:


To initialize a bitarray from a byte string:


To get or set a specific bit in the bitarray:

ba[9] = 0

Tried with: Bitarray 0.8.0, Python 3.4 and Ubuntu 14.04


How to name a Python file

Every Python source file can essentially be considered as a module. The following rule applies to the name of a module, if it needs to be imported without problems:

(letter|"_") (letter | digit | "_")*

That is, the rule for naming a Python file is the same as naming a variable in it. First character has to be letter or underscore, followed by any number of letters, digits or underscores. No other characters, like dash, are allowed.

If the file is not named correctly, the following error or warning might be generated on importing it:

Invalid name for Python module

Tried with: Python 2.7.6 and Ubuntu 14.04

How to install CPAN modules

CPAN hosts thousands of Perl modules that can be installed easily on your computer. The traditional method to install a module named foo::bar is by using the cpan command:

$ cpan foo::bar

However, the tool asks you to answer quite a few questions to go through with the module installation.

A highly recommended improvement to CPAN module installation is to use cpanminus. To do this first install cpanminus using cpan:

$ cpan App::cpanminus

After this, you can install any CPAN module by calling cpanm:

$ cpanm foo::bar

The advantage of using cpanminus is that it takes care of automating the module installation process as much as possible for you.

Tried with: CPAN 1.57 and Cygwin 1.7.25

How to view modules loaded by a Python program

The Python program has a useful parameter: -v When Python is launched with this parameter to run a Python program, it prints out all the Python modules and dynamic libraries that are loaded during the executing. This can be useful to see exactly which Python files and shared library files are loaded and in which order. This parameter also prints out the modules as they are cleaned up at the end of program execution.

$ python -v

Tried with: Python 2.7.3 and Ubuntu 12.04 LTS

Python version not found in registry error


Once you start installing many Python modules, you are sure to run into this error. When installing a Python module, it pops up this error:

Python version 3.2 required, which was not found in the registry.

I do have Python 3.2 installed, so what’s up?


The problem is that some module installers look in the wrong place for Python version information. For example, in the case of this one module, the installer was looking for HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.2 in the registry. I found that my Python 3.2 installer had placed this information in HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.2 instead.

Once you know the reason, solving it is easy. Open Registry Editor (may need administrator privilege) and export HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.2 into a text file. Open the exported .reg file in a text editor and replace the HKEY_LOCAL_MACHINE entries to HKEY_CURRENT_USER. Add these new entries back to the registry (here is how). The module installer should now be able to proceed without throwing the error.

Tried with: Python 3.2 64-bit on Windows 7