由于python-for-android的编译的pandas库并不能在android中运行,所以发布这篇博客,记录如何编译pandas.
1 工作目录
当前目录:~/apk-ex/pandas-setup
代码目录:~/apk-ex/pandas-src
虚拟环境:~/apk-ex/venv
依赖库:kivy[base]==2.2.1
2 安装软件
2.1 安装pandas和Cython
python -m pip install pandas -i https://mirrors.aliyun.com/pypi/simple
python -m pip install Cython==3.0.10 -i https://mirrors.aliyun.com/pypi/simple
2.2 下载并运行补丁
补丁下载地址: https://download.csdn.net/download/RainbowPY/89607631
运行脚本 pandas_patch.py
3 编写代码
3.1 测试代码 ~/apk-ex/pandas-src/main.py
import pandas as pd
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
import numpy as np
class BoxLayoutWidget(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 创建一个简单的DataFrame
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 23, 34, 29],
'City': ['New York', 'Paris', 'Berlin', 'London']
}
df = pd.DataFrame(data)
dispstr = ''
# 选择列
dispstr = str(df['Name']) + '\n'
dispstr += str(df.Name) + '\n'
# 选择多列
dispstr += str(df[['Name', 'Age']]) + '\n'
# 过滤行
dispstr += str(df[df['Age'] > 30]) + '\n'
# 排序
dispstr += str(df.sort_values(by='Age')) + '\n'
# 聚合
dispstr += str(df.groupby('City').count()) + '\n'
btn = Button(text=dispstr)
self.add_widget(btn)
class TestApp(App):
def build(self):
return BoxLayoutWidget()
if '__main__' == __name__:
TestApp().run()
![](https://i-blog.csdnimg.cn/direct/3bd5cc7ea14b42e187d94536ff6e5bfd.png)
3.2 安装脚本 ~/apk-ex/pandas-setup/setup.py
from setuptools import setup
from setuptools import find_packages
import os
os.environ['ANDROIDSDK'] = os.path.abspath('../android-sdk')
os.environ['ANDROIDNDK'] = os.path.abspath('../android-sdk/ndk/25.2.9519653')
os.environ['ANDROIDAPI'] = '26'
os.environ['NDKAPI'] = '21'
options = {'apk': {'requirements': 'python3, sdl2, kivy, numpy, pandas',
'debug': None,
'dist-name': 'pandas-setup',
'package': 'org.apk.example',
'bootstrap': 'sdl2',
'arch': 'arm64-v8a'}}
setup(
name='pandas-test',
version='0.0',
description='pandas-for-android setup',
author='C+ detective',
packages=find_packages(),
options=options,
package_data={'../pandas-src': ['*.py']}
)
4 运行安装脚本
python setup.py apk
生成android安装包:pandas-setup-debug-0.0.apk
il, urllib3, idna, chardet, pytz, certifi, requests)>]
[DEBUG]: Dist matching ndk_api and recipe: [<Distribution: name pandas-setup with recipes (hostpython3, libbz2, libffi, liblzma, openssl, sdl2_image, sdl2_mixer, sdl2_ttf, sqlite3, python3, sdl2, setuptools, cython, six, numpy, pyjnius, android, kivy, pandas, python-dateutil, urllib3, idna, chardet, pytz, certifi, requests)>]
[INFO]: Of the existing distributions, the following meet the given requirements:
[INFO]: Of the existing distributions, the following meet the given requirements:
[INFO]: pandas-setup: min API 21, includes recipes (hostpython3, libbz2, libffi, liblzma, openssl, sdl2_image, sdl2_mixer, sdl2_ttf, sqlite3, python3, sdl2, setuptools, cython, six, numpy, pyjnius, android, kivy, pandas, python-dateutil, urllib3, idna, chardet, pytz, certifi, requests), built for archs (arm64-v8a)
[INFO]: pandas-setup: min API 21, includes recipes (hostpython3, libbz2, libffi, liblzma, openssl, sdl2_image, sdl2_mixer, sdl2_ttf, sqlite3, python3, sdl2, setuptools, cython, six, numpy, pyjnius, android, kivy, pandas, python-dateutil, urllib3, idna, chardet, pytz, certifi, requests), built for archs (arm64-v8a)
[INFO]: pandas-setup has compatible recipes, using this one
[INFO]: pandas-setup has compatible recipes, using this one
[INFO]: # Copying android package to current directory
[INFO]: # Copying android package to current directory
[INFO]: # Android package filename not found in build output. Guessing...
[INFO]: # Android package filename not found in build output. Guessing...
[INFO]: # Found android package file: /home/pretzel/.local/share/python-for-android/dists/pandas-setup/build/outputs/apk/debug/pandas-setup-debug.apk
[INFO]: # Found android package file: /home/pretzel/.local/share/python-for-android/dists/pandas-setup/build/outputs/apk/debug/pandas-setup-debug.apk
[INFO]: # Add version number to android package
[INFO]: # Add version number to android package
[INFO]: # Android package renamed to pandas-setup-debug-0.0.apk
[INFO]: # Android package renamed to pandas-setup-debug-0.0.apk
[DEBUG]: -> running cp /home/pretzel/.local/share/python-for-android/dists/pandas-setup/build/outputs/apk/debug/pandas-setup-debug.apk pandas-setup-debug-0.0.apk
[DEBUG]: -> running cp /home/pretzel/.local/share/python-for-android/dists/pandas-setup/build/outputs/apk/debug/pandas-setup-debug.apk pandas-setup-debug-0.0.apk
<Command '/usr/bin/cp /home/pretzel/.local/share/python-for-android/dists/pandas-setup/build/outputs/apk/debug/pandas-setup-debug.apk pandas-setup-debug-0.0.apk', pid 128561>: process started
(venv) pretzel@mx:~/apk-ex/pandas-setup
$