TLDR: Python Dev Dependencies on Ubuntu

Published on

Some libraries aren’t 100% packaged Python code, some of them are bindings to external libraries. They offer a Python interface so you can call them inside your code. On these cases, we need to install them previously at the system so the python package can work.

When that happens, we get this kind of error:

ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3

To solve errors like that, you need to install the external library, and the system configures everything to you. However, if you work with multiple Python versions managed by pyenv, you have to rebuild all python versions every time that you remember to install a new dependency.

To avoid suffering from that, after installing the OS, and before mounting your Python environment, I like to install all the main external dependencies that I’m used to using:

$ sudo apt-get install dpkg-dev \
                       build-essential \
                       python-dev \
                       python3-dev \
                       libncursesw5-dev \
                       libsqlite3-dev \
                       libreadline-dev \
                       libbz2-dev \
                       libffi-dev \
                       libssl-dev \
                       libgdbm-dev \
                       zlib1g-dev \
                       libjpeg-dev \
                       libtiff-dev \
                       libpq-dev \
                       libxml2-dev \
                       libxslt1-dev \
                       libsdl2-dev \
                       libgstreamer-plugins-base1.0-dev \
                       libnotify-dev \
                       freeglut3-dev \
                       libsm-dev \
                       libgtk-3-dev \
                       libwebkitgtk-3.0-dev \
                       libxtst-dev

This command was tested at Ubuntu 18.04, the names of the packages can change from one version to another.

Bonus

For everyone that wants to know the use cases of each dependency, I made a breakdown describing them:

$ sudo apt-get install dpkg-dev build-essential

Installs the C and C++ compilers, also the make command used to compile the CPython dependencies.

$ sudo apt-get install python-dev python3-dev

Installs the headers and static libraries of python 2 and 3 that are used to compile and mount the language extensions.

$ sudo apt-get install libncursesw5-dev

Library used on packages that deal with the terminal interface, like visidata for example.

$ sudo apt-get install libsqlite3-dev libreadline-dev libbz2-dev

Libraries used to work with SQLite databases.

$ sudo apt-get install libffi-dev libssl-dev

Libraries used on packages which implement OpenSSL.

$ sudo apt-get install libgdbm-dev

The base of the most of packages that handle databases.

$ sudo apt-get install zlib1g-dev libjpeg-dev libtiff-dev

These are external dependencies of packages used for image processing like Pillow.

$ sudo apt-get install libpq-dev

It is the dependency of psycopg2 to work with PostgreSQL databases.

$ sudo apt-get install libxml2-dev libxslt1-dev

Libraries used to handle XML on packages like lxml.

$ sudo apt-get install dpkg-dev \
                       build-essential \
                       libjpeg-dev \
                       libtiff-dev \
                       libsdl2-dev \
                       libgstreamer-plugins-base1.0-dev \
                       libnotify-dev \
                       freeglut3-dev \
                       libsm-dev \
                       libgtk-3-dev \
                       libwebkitgtk-3.0-dev \
                       libxtst-dev

This group is used to install packages that work with window management and graphical interfaces like wxPhoenix. On a previous post, I taught how to organize a coding dojo, and I told about a TDD semaphore called dose that uses wxPhoenix to run at Python 3 environments.

Installing these dependencies you’ll be able to install it without problems.

comments powered by Disqus