p4a Cannot Import Module _ctypes

Question:

I am an extreme beginner with kivy and python-for-android. I’ve created a kivy tutorial app and have been trying to create the apk using p4a. I believe I’ve figured out the dependencies and command line requirements but I am currently stuck with a failure due to "_ctypes module not found" that according to the error messages occurs during an import attempt. The _ctypes module is installed and I am able to import it in IDLE without any problems. My OS is Linux Ubuntu 22.04.

The kivy tutorial app is the "Pong" app. It works perfectly on my PC and I don’t think that is the issue, but can post its code if anyone suspects otherwise.

Using Python-for-Android, starting in my working directory, the p4a command I am entering is "p4a apk –private main.py –package=org.ach.pong –name "Pong" –version 0.1 –bootstrap=sdl2 –requirements=python3,kivy –arch arm64-v8a"

After the p4a compiling process runs for a few minutes, it always fails with the error "ModuleNotFoundError: No module named ‘_ctypes’"

But, as stated above, if I open IDLE and import _ctypes, it works perfectly, so I don’t understand why the p4a compiler isn’t finding it?

Asked By: ACH

||

Answers:

Okay, I finally found the answer. Please bear with me here in as I’ll detail it step by step in case anyone else has a similar problem.

This looks like it was added very recently, but, from https://python-for-android.readthedocs.io/en/latest/troubleshooting/#modulenotfounderror-no-module-named-ctypes

"ModuleNotFoundError: No module named ‘_ctypes’
You do not have the libffi headers available to python-for-android, so you need to install them. On Ubuntu and derivatives these come from the libffi-dev package.
After installing the headers, clean the build (p4a clean builds) and run python-for-android again."

I had definitely already installed that package, but, didn’t realize you need to "clean the build" I used, from their "Getting Started" page "If you just want to clean the builds to avoid redownloading dependencies, run: p4a clean_builds && p4a clean_dists"

But I probably could have gotten away with just "p4a clean builds" which I learned after fixing the _ctypes problem because my next problem was: "SSLError(“Can’t connect to HTTPS URL because the SSL module is not available.”)" This was also covered in the p4a troubleshooting section and, apparently, after that first way I did the "clean_builds" I had to RE-install libssl-dev. After re-installing that, I had to do that "p4a clean builds" and then it seemed like it ALMOST got there.

My final failure was "No main.py(o) found in your app directory. This file must exist to act as the entry point for your app. If your app is started with a file with a different name, rename it to main.py or add a main.py that loads it." This was confusing because that was my filename. But, it dawned on me that I need to use the file folder location in the p4a compile script, and not, as I originally assumed, the filename ‘main.py’. So:

"p4a apk –private main.py –package=org.ach.pong –name "Pong" –version 0.1 –bootstrap=sdl2 –requirements=python3,kivy –arch arm64-v8a"

needed to be changed to:

"p4a apk –private $HOME/Documents/Kivy/kivy_venv/Pong –package=org.ach.pong –name "Pong" –version 0.1 –bootstrap=sdl2 –requirements=python3,kivy –arch arm64-v8a"

NOW, it finally worked. Thank you to anyone reading this far and I hope this also helps someone else get their first app compiled!

Answered By: ACH
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.