How to make Python Packages part of Yocto generated Image?

Hamza Ali Imran
Emumba
Published in
5 min readNov 3, 2019

--

Today I’m going to share with you my experience of adding Python Packages to Yocto Generated Images for any Embedded Device. I’m going to share with you details of a few tools that will make your task super easy. I call them magic tools. This blog is going to be in a tutorial format in which I will show you all the steps of adding any Python Package to Yocto Image and will going to add “numpy” to Yocto image for Raspberry Pi 3.

Majority of you may have used “pip” for installing Python tools and Packages. It is a package manager for installing Python Packages, something similar to “apt” or “yum”. It downloads packages from this link. The method I’m going to show you today is valid for all packages available here. So let’s get started.

Checking Package Name

This may sound strange, but we first need to know the exact name of a package; for that go to this link. And type the name of the package you are searching or interested in.

Then it will show you the matching cases to your keyword. Open the page of one which you think is a suitable one. There will be a command of “pip” mentioned on the page. The exact name of the package will be mentioned in this command you have to copy it. For example

Creating Yocto Layer

Now you have to create a layer in which you will be adding the recipe of the tool or package you intend to make part of Yocto Image. Note this tutorial is expecting that you know the basics of Yocto hence is not containing any details of the basics working mechanism of Yocto.

For adding a new layer the easiest way is to use Bitbake create-layer command. Open the terminal, move to the directory of “poky” and source the “oe-init-build-env” script and type the following command

bitbake-layers create-layer ../meta-blog

This will create a basic directory structure for your new layer which for demo purpose named “meta-blog”. The tree of this directory is shown below

This layer contains an MIT licence file, a demo README file which contains no useful information, a demo recipe named example which is also not useful so you can delete “recipes-example” folder right away. The folder named “conf” contains the “layer.conf” file which contains the information telling Yocto to make use of all the recipes inside this layer. Moreover, this file also set the priority of the layer and the default number is “6”. Now as our layer is ready we can create the recipes inside it. For that, we are going to use “pipoe” which is a magic tool and makes our task super easy.

Creating Recipe of Python Package

The easiest tool to write the recipe of any Python package which is available at “pypi.org” repository is using “pipoe” utility. This utility itself is present at “PyPI” repository. So first you need to install it on your system. You can install it using pip like following

pip install pipoe

But if you use Anaconda like me (common case with people working on Data Science or Machine Learning) then you can install pipoe by “conda” package manager. For that simply type the following command on terminal

conda install pipoe

After the installation completes you should restart your system. Although Python packages work right after they are installed on the system but I found out that pipoe does not work until you reboot your system once after installation. To check the installation was successful or not. You can type the following command

pipoe -h

This will printout the help menu of pipoe and you will see something similar to this

If this is the case then you have done everything correctly and you are ready to create the recipe for “numpy”. For that go the directory of your layer and create a directory named “recipes-numpy” move into it and create another directory named “numpy”. Move to this directory and type following command

pipoe — package numpy

pipoe will start to create a recipe for your require python package. As soon as the process complete you can check the files created by pipoe. There will be two files created by pipoe which will be“python-numpy_1.17.3.bb” and “python-versions.inc”. “.bb” file will contain the information including from where to clone the package source files, checksum, author details, license details etc. “.inc” file contains the information of the version of a package that will be cloned by Yocto while making use of this recipe. And that is it. All the work which you would have to do for creating the recipe is taken care of by “pipoe”. This is the reason I call it a magic tool.

Adding Your recipe to the Image

Adding a recipe of Python Package to Yocto image is slightly different then ordinary C/C++ made tools. The first step is the same that is to add the layer to your “bblayers.conf” file. For that, the easiest way is to use the following command

bitbake-layers add-layer ../meta-blog

and to check it became the part or not type the following command

bitbake-layers show-layers

Now you need to add the name of the tool in “local.conf” file like this

Note the word “python3” is added with the name of the package. This is the only change in adding Python and C++ based tools. If you want to use the Python3 based tool type the word “python3-” else for Python2 type “python-” at the beginning of the name of the package. Now you can start the compilation of Image. This package will become part of your image.

Note

Please note that “pipoe” creates recipes for the majority of the Python Packages but in some cases, you have to make minor changes in the recipe to make it work the way you intend but still “pipoe” is a useful tool because it will reduce 90 percent your effort.the

--

--