Attention: Python Foundation announced the sunsetting of Python 2. Although Python2 is still available from Debian and a thousand sources, it's not recommended for a new application. You must use Python 3 to receive updates, bug fixes, and security patches.
To better understand this article, you are encouraged to also read:
Check out the main differences between using a Python container and installing Python to a TorizonCore base container.
The official python account on the docker hub provides some base containers that include the Python interpreter, those are based on different Linux distributions (Alpine, Debian) and work on many architectures, including arm32 and arm64. They would run on TorizonCore but may not be the best solution in case you want to run Python applications on Toradex devices.
We provide some debian-based containers that include Torizon-specific improvements, like supporting the same user/group accounts used by the underlying OS, learn more about our images on our Debian Containers for Torizon article.
If you want to use the Python interpreter on our images, it's easy: add a single command to your Dockerfile:
RUN apt-get update \ && apt-get install -y --no-install-recommends \ python3 \ python3-pip \ python3-setuptools \ && rm -rf /var/lib/apt/lists/*
Note: You may want to remove pip and setup tools if your code does not depend on any external package.
Adding these commands to your Dockerfile will restrict Python version to 3 (provided by Debian), this may prevent you from using the latest bleeding-edge releases. You will use a long-term maintained and probably very popular release of the runtime, which is the same used by most PC users running Debian-based distros, also getting security bugfixes.
Debian also provides lots of pre-built packages for many popular Python libraries and this can really make your development easier.
One of the best features of Python is its very rich ecosystem of libraries and components, which can be easily installed using the pip package manager. This, of course, works also in a container on TorizonCore and specifically on our Debian containers.
However, some packages may have additional requirements about the host OS, like having some libraries available. Others may include binaries that are not compatible with our platform or have some native code they need to build on the target, requiring a full build environment there.
Fortunately, Debian provides many Python components as Debian packages and this can really help to make the installation more straightforward and consuming fewer resources. Those packages are also in versions that are known to be compatible with the interpreter and libraries on the system.
In short, if you want to install a Python component that has some native dependencies (includes native code or calls native libraries), check first if there is a Debian package for it on Debian Packages.
Note: If the package is pure Python then using pip or Debian packages should not make a real difference.
Virtual environments are commonly used to have separate configurations on Python (different versions, different libraries) for different applications. This does not make much sense in a container, where you can set up your own environment and keep it separated from the rest of the system. Packages installed via Debian would probably not work inside a virtual environment.
Toradex provides a solution to debug Python code running in a TorizonCore container using Visual Studio Code.
This will automate the build, deployment of the container image and allow you to do step-by-step debugging of your code running on the target: