Published by bp99 on Fri Aug 16 11:23:40 2019

bp99

Tell pip include and library path

TL; DR

Use something like the following:

$ pip install \
      --global-option=build_ext \
      --global-option=-I/path/to/include \
      --global-option=-L/path/to/libs \
      --install foo

Diagnosis

When installing a package via pip, a special include path and/or library path must be specified.

If I were building a C project, I would just pass the -I and -L flag. However, this time, I am building with python using pip, eg:

$ pip install --user foo

Specific case: I was trying to install the olm library for weechat-matrix and got this (reformatted for readability):

[...]

running install
    running build
    running build_py
    creating build
    creating build/lib.openbsd-6.5-amd64-2.7
    creating build/lib.openbsd-6.5-amd64-2.7/olm
    copying olm/__init__.py -> build/lib.openbsd-6.5-amd64-2.7/olm
    copying olm/__version__.py -> build/lib.openbsd-6.5-amd64-2.7/olm
    copying olm/_compat.py -> build/lib.openbsd-6.5-amd64-2.7/olm
    copying olm/_finalize.py -> build/lib.openbsd-6.5-amd64-2.7/olm
    copying olm/account.py -> build/lib.openbsd-6.5-amd64-2.7/olm
    copying olm/group_session.py -> build/lib.openbsd-6.5-amd64-2.7/olm
    copying olm/pk.py -> build/lib.openbsd-6.5-amd64-2.7/olm
    copying olm/sas.py -> build/lib.openbsd-6.5-amd64-2.7/olm
    copying olm/session.py -> build/lib.openbsd-6.5-amd64-2.7/olm
    copying olm/utility.py -> build/lib.openbsd-6.5-amd64-2.7/olm
    running build_ext
    generating cffi module 'build/temp.openbsd-6.5-amd64-2.7/_libolm.c'
    creating build/temp.openbsd-6.5-amd64-2.7
    building '_libolm' extension
    creating build/temp.openbsd-6.5-amd64-2.7/build
    creating build/temp.openbsd-6.5-amd64-2.7/build/temp.openbsd-6.5-amd64-2.7

    cc -pthread -fno-strict-aliasing -OPT:Olimit=0 -O2 -pipe -DNDEBUG -O2 [...]

    build/temp.openbsd-6.5-amd64-2.7/_libolm.c:493:18:
    fatal error: 'olm/olm.h' file not found
            #include <olm/olm.h>
                     ^~~~~~~~~~~
    1 error generated.
    error: command 'cc' failed with exit status 1

    ----------------------------------------

This makes sense, since I only have the olm.h header somewhere in a local git repository under ~.

Solution

I cannot explain in detail, but flags to cc for example (or any compiler I guess) can be given to pip using the --global-option option and something called build_ext. First, tell pip --global-option=build_ext, then specify all options to the CC using --global-option=foobar, one by one. To build olm, I did this:

$ pip install \
        --global-option=build_ext \
        --global-option=-I$HOME/include \
        --global-option=-L$HOME/lib \
        [the rest of the install command]