Kivy APK with pickle

653 views
Skip to first unread message

Raphael Benedict Luta

unread,
Feb 14, 2018, 5:02:09 AM2/14/18
to Kivy users support
Hi! I have been trying to create an APK through buildozer that uses the pickle library. However, buildozer fails to compile the APK file, and it seems to me that the reason for this is that there is no current recipe for pickle. I just wanted to ask if there is any way to make this work without needing to create a recipe? I have seen posts from 2 years ago from people who have used pickle for creating APKs with no issues. Thank you for anyone who can help me!

ZenCODE

unread,
Feb 14, 2018, 12:59:36 PM2/14/18
to Kivy users support
pickle is a part of the standard python libraries, so works with kivy no problem. We use it all the time. No recipe is required. So perhaps start from the beginning. What is your exact problem? What exactly are you trying to do? What exactly is the error message?

The emphasis here in on "exact". "I Have a problem! How do I fix it?" is meaningless. Life is full of problems. We need to know which one you need to fix...:-)

Message has been deleted
Message has been deleted

Raphael Benedict Luta

unread,
Feb 15, 2018, 2:58:34 AM2/15/18
to Kivy users support
Ok, I will describe my problem as accurate as possible. I have been trying to include a function in my kivy app that uses the pickle module, with the function as shown below:

    def Classify(Z):      
       
       
with open("cat.pkl", "rb") as fp:   # Unpickling
            triagetext
= pickle.load(fp)
               
       
return triagetext

The app runs with no problems on desktop. At first, I tried creating the apk without adding pickle to the dependencies. The buildozer was able to create the APK successfully, but the mobile app crashes when I press the button that runs the function above. Next, I tried adding pickle to the requirements in the buildozer.spec file, but the buildozer fails to compile it with the error log below. The requirements for my app are hostpython2, kivy, and pickle.


[INFO]:    # Installing pure Python modules
[INFO]:    The requirements (pickle) don't have recipes, attempting to install them with pip
[INFO]:    If this fails, it may mean that the module has compiled components and needs a recipe.
[INFO]:    -> directory context /home/kivy/Downloads/.buildozer/android/platform/build/build
[INFO]:    -> running virtualenv --python=python2.7 venv
objs_paths are ['
/home/kivy/Downloads/.buildozer/android/platform/build/build/bootstrap_builds/sdl2_gradle-python2/collated_objects']
Copying libraries
           Exception in thread background thread for pid 26683: 83, in copyfile
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 2170, in background_thread
    handle_exit_code(exit_code)
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 1929, in fn
    return self.command.handle_command_exit_code(exit_code)
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 672, in handle_command_exit_code
    raise exc
ErrorReturnCode_1:
  RAN: /usr/local/bin/virtualenv --python=python2.7 venv
  STDOUT:
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /home/kivy/Downloads/.buildozer/android/platform/build/build/venv/bin/python2.7
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 2328, in <module>
    main()
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 713, in main
    symlink=options.symlink)
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 925, in create_environment
    site_packages=site_packages, clear=clear, symlink=symlink))
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 1231, in install_python
    shutil.copyfile(executable, py_executable)
  File "/usr/lib/python2.7/shutil.py", line 83, in copyfile
    with open(dst, '
wb') as fdst:
IOError: [Errno 26] Text file busy: '
/home/kivy/Downloads/.buildozer/android/platform/build/build/venv/bin/python2.7'
  STDERR:
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/kivy/Downloads/.buildozer/android/platform/python-for-android-new-toolchain/pythonforandroid/toolchain.py", line 978, in <module>
    main()
  File "/home/kivy/Downloads/.buildozer/android/platform/python-for-android-new-toolchain/pythonforandroid/toolchain.py", line 975, in main
    ToolchainCL()
  File "/home/kivy/Downloads/.buildozer/android/platform/python-for-android-new-toolchain/pythonforandroid/toolchain.py", line 512, in __init__
    getattr(self, args.subparser_name.replace('
-', '_'))(args)
  File "/home/kivy/Downloads/.buildozer/android/platform/python-for-android-new-toolchain/pythonforandroid/toolchain.py", line 149, in wrapper_func
    build_dist_from_args(ctx, dist, args)
  File "/home/kivy/Downloads/.buildozer/android/platform/python-for-android-new-toolchain/pythonforandroid/toolchain.py", line 193, in build_dist_from_args
    build_recipes(build_order, python_modules, ctx)
  File "pythonforandroid/build.py", line 592, in build_recipes
  File "pythonforandroid/build.py", line 611, in run_pymodules_install
  File "pythonforandroid/logger.py", line 175, in shprint
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 720, in next
    self.wait()
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 651, in wait
    self.handle_command_exit_code(exit_code)
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 672, in handle_command_exit_code
    raise exc
sh.ErrorReturnCode_1:
  RAN: /usr/local/bin/virtualenv --python=python2.7 venv
  STDOUT:
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /home/kivy/Downloads/.buildozer/android/platform/build/build/venv/bin/python2.7
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 2328, in <module>
    main()
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 713, in main
    symlink=options.symlink)
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 925, in create_environment
    site_packages=site_packages, clear=clear, symlink=symlink))
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 1231, in install_python
    shutil.copyfile(executable, py_executable)
  File "/usr/lib/python2.7/shutil.py", line 83, in copyfile
    with open(dst, '
wb') as fdst:
IOError: [Errno 26] Text file busy: '
/home/kivy/Downloads/.buildozer/android/platform/build/build/venv/bin/python2.7'
  STDERR:
# Command failed: /usr/bin/python -m pythonforandroid.toolchain create --dist_name=TriagePickle4 --bootstrap=sdl2 --requirements=hostpython2,kivy,pickle --arch armeabi-v7a --copy-libs --color=always --storage-dir=/home/kivy/Downloads/.buildozer/android/platform/build
#
# Buildozer failed to execute the last command
# The error might be hidden in the log above this error
# Please read the full log, and search for it before
# raising an issue with buildozer itself.
# In case of a bug report, please add a full log with log_level = 2
kivy@kivyvm:~/Downloads$


ZenCODE

unread,
Feb 16, 2018, 4:38:32 AM2/16/18
to Kivy users support
You definitely don't need to add pickle to the requirements. Try putting you pickle.load in a try/except block. Could it be the pickle is created with a different version of python to the version used in the apk? That can cause issues. Also, try specifying the protocol version for the pickle, just to be safe. 

If it still throws an error use the except clause to display the error? 

Raphael Benedict Luta

unread,
Feb 16, 2018, 6:37:00 AM2/16/18
to Kivy users support
Alright, I tried the options that you suggested. I specified the pickling protocol and removed the pickle to the requirements.

At first I tried using the first code below, but the mobile app still crashes when I push the button for the function.

First code:
        try:
           
with open("cat.txt", "rb") as fp:   # Unpickling
                triagetext
= pickle.load(fp)
       
           
       
except Exception as ex:
           
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
            triagetext
= template.format(type(ex).__name__, ex.args)
           
       
return triagetext


It seems that the APK cannot use the try/except blocks? In order to make sure, I used the second code below where the program opens a wrong file name ("cat.tt" instead of "cat.txt" which should result in a FileNotFoundError, however, the APK version of the program still just crashes when the function is run.

Second code:
        try:
           
with open("cat.tt", "rb") as fp:   # Unpickling
                triagetext
= pickle.load(fp)
                   
       
except:
            triagetext
= "gg"
           
       
return triagetext

It seems that the try/except blocks do not work for the APK versions? Could it be that my buildozer is bugged or corrupted? Thank you for your help by the way, hope I resolve this problem soon!

Alexander Taylor

unread,
Feb 16, 2018, 7:13:43 PM2/16/18
to Kivy users support
Reply all
Reply to author
Forward
0 new messages