6. Install a Python Package in Yocto
Your project is going well, and your application is working beautifully, but now you want to add a new, wonderful package to your project.
While installing packages using pip install is simple on your computer, you need to follow a different approach in Yocto if you want to customize your own image while keeping it small.
Before proceeding, make sure you understand the tutorial First Steps with Yocto.
6.1. Check if the recipe is already available
You can check if a recipe is already available by going inside the sources folder and doing:
find -name "my-lib"
NOTE You can also check the OpenEmbedded Website for available recipes.
6.2. Add a recipe
If Yocto provides a recipe for your package, the integration process is straightforward. Here’s how:
Open your
conf/local.conffileAdd the following line to include the package in your image:
IMAGE_INSTALL += " python3-my-lib"
Replace
my-libwith the actual name of your packageBuild your Yocto image to incorporate the package with:
bitbake my-image
6.3. Create your recipe with PyPI
If you don’t have an existing recipe and the package is available on PyPi, you have the power to create your own.
6.3.1. Prerequisites
Make sure you have your own layer installed and configured before creating your recipe. If it’s not the case, check the tutorial Creating Your Own Layer with Yocto.
6.3.2. Creating the recipe
Find the package version you need on the PyPI website
Choose a version for you package and name your recipe following this format:
mylib_1.2.3.bb, replacing ‘mylib’ with the package name and ‘1.2.3’ with the desired versionCreate a new recipe with the following content:
SUMMARY = "MyLib Python package"
HOMEPAGE = "https://example.com/mylib"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI[md5sum] = "1234567890abcdef1234567890abcdef"
# or
# SRC_URI[sha256sum] = "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"
inherit pypi setuptools3
Replace the
LICENSE,LIC_FILES_CHKSUMandSRC_URIvalues with the actual checksums you obtained from the PyPI websiteCheck the dependencies needed for your library and add them to your Yocto setup
Try building the recipe using the bitbake command:
bitbake mylib
If everything is working fine, that’s great! You have successfully created a recipe for your package.
6.3.3. Troubleshooting
Running into issues? Don’t worry, it happens to the best of us. Here are some tips:
Ensure that all dependencies are correctly specified in the recipe and check the recipe of those dependencies
Create a patch to fix the broken code
To learn more on recipes, check the openembedded meta-python layer.
6.4. Conclusion
Yocto offers multiple ways to add Python packages, whether through existing recipes or custom recipes.
Remember to refer to the official Yocto documentation and the PyPI website for valuable insights: