Table of Contents
install kivy for centos7
Develop first with kivy A Python app
Create a main.py file and write:
Create a hello.kv file and write:
Run the first Python app

If you encounter an error during the packaging process, you can modify the log_level in the buildozer.spec configuration file to 2, and then run it again. You can see the specific error information.

This error was reported when I ran it on centos7. The general idea is that the system is missing some 32bits. Dependency files.

The error is to the effect that there is an error in the cython file. It may be that the cython module is not installed. , or there is a problem with the version. Solution:
Home Backend Development Python Tutorial How to use Python to develop apps

How to use Python to develop apps

May 06, 2023 pm 12:28 PM
python

Preparation

Using Python to develop apps requires the use of a Python module – kivy. kivy is an open source, cross-platform Python development framework for development Use innovative apps. In short, this is a Python desktop program development framework (similar to wxpython and other modules). The powerful thing is that kivy supports linux, mac, windows, android, and ios platforms. This is why this module is needed to develop apps.

Although kivy is cross-platform, if you want to use Python code on different platforms, you also need to package the Python code into an executable program for the corresponding platform. Fortunately, there is a packaging tool under the kivy project Project – buildozer, this is the officially recommended packaging tool because it is relatively simple and has a high degree of automation. Other projects such as Python-for-android can also play a similar role and will not be introduced here.

Building kivy development environment

You need to install the kivy development environment on your PC. Here is a demonstration of the installation process under mac and linux.

install kivy for mac
Copy after login

Install some dependent packages:

brew install pkg-config sdl2 sdl2_image sdl2_ttf sdl2_mixer gstreamer
Copy after login

Install cython and kivy:

pip install cython==0.25
pip install kivy
Copy after login

If an error is reported when installing kivy, use the following method to install kivy:

git clone https://github.com/kivy/kivy
python setup.py install
Copy after login

Post-installation test:

$python
Python 2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import kivy
[INFO ] [Logger] Record log in /Users/didi/.kivy/logs/kivy_18-05-08_4.txt
[INFO ] [Kivy] v1.10.1.dev0, git-5f6c66e, 20180507
[INFO ] [Python] v2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
Copy after login

Note: If there is no error when importing the kivy module, it means the installation is successful.

install kivy for centos7

First install dependencies:

yum install 
make 
mercurial 
automake 
gcc 
gcc-c++ 
SDL_ttf-devel 
SDL_mixer-devel 
khrplatform-devel 
mesa-libGLES 
mesa-libGLES-devel 
gstreamer-plugins-good 
gstreamer 
gstreamer-python 
mtdev-devel 
python-devel 
python-pip 
java-devel
Copy after login

Install cython and kivy:

pip install Cython==0.20
pip install kivy
Copy after login

Develop first with kivy A Python app

After installing kivy, you can develop an app program. Here is a demonstration of the hello-world program. The more complex usage of kivy is not the focus of this article and will be introduced in writing later.

Create a main.py file and write:
#! -*- coding:utf-8 -*-
from kivy.app import App
class HelloApp(App):
pass
if __name__ == '__main__':
HelloApp().run()
Copy after login
Create a hello.kv file and write:
Label:
text: 'Hello, World! I am nMask'
Copy after login

Simple Note: main.py is the entry function and defines a HelloApp class, which inherits kivy.app; the hello.kv file is a kivy program, which is equivalent to defining the interface style, etc. The naming rule of this file is that the class name is in lowercase and app is removed.

Run the first Python app

python main.py
Copy after login

Run result:

How to use Python to develop apps

##Install the buildozer tool


Through the above coding, I created my first python app program. This program can run directly on mac, linux, and windows platforms. So how do I make it run on Android or Apple phones? Woolen cloth? We know that to run on Android, it needs to be packaged into an apk installation program, so we need to use the buildozer tool mentioned earlier. (The buildozer tool can package kivy programs and supports android, ios, etc.). The installation process of buildozer is relatively simple. :


pip install buildozer
Copy after login

Use the buildozer tool to package the kivy program into an apk


Run it in the python project directory:


buildozer init
Copy after login

If the operation is successful, it will Create a configuration file buildozer.spec. You can change the name of the app by modifying the configuration file, and then run:


buildozer android debug deploy run
Copy after login

Running the above command will generate a cross-platform installation package, applicable to Android, ios, etc. , if used for Android, use the python-for-android project.


When you run the above command for the first time, necessary files such as Android sdk will be automatically downloaded in the system, as shown below. (The process requires overcoming the firewall, and there are many dependencies that need to be downloaded)


How to use Python to develop apps

Note: Here we only demonstrate packaging into apk files, and you can study it on your own if you are using the iso platform.


python apk program test


If the above steps are successful, an apk file should be generated in the bin directory under the project directory, similar to the following:


How to use Python to develop apps

Then download the apk to your Android phone and install it. The test results are as follows:


How to use Python to develop apps

Open the app


How to use Python to develop apps##buildozer instructions

Usage:
buildozer [--profile] [--verbose] [target]...
buildozer --version
Available targets:
androidAndroid target, based on python-for-android project
iosiOS target, based on kivy-ios project
android_oldAndroid target, based on python-for-android project (old toolchain)
Global commands (without target):
distcleanClean the whole Buildozer environment.
help Show the Buildozer help.
init Create a initial buildozer.spec in the current directory
serveServe the bin directory via SimpleHTTPServer
setdefault Set the default command to run when no arguments are given
versionShow the Buildozer version
Target commands:
cleanClean the target environment
update Update the target dependencies
debugBuild the application in debug mode
releaseBuild the application in release mode
deploy Deploy the application on the device
runRun the application on the device
serveServe the bin directory via SimpleHTTPServer
Target "android_old" commands:
adbRun adb from the Android SDK. Args must come after --, or
use --alias to make an alias
logcat Show the log from the device
Target "ios" commands:
list_identitiesList the available identities to use for signing.
xcodeOpen the xcode project.
Target "android" commands:
adbRun adb from the Android SDK. Args must come after --, or
use --alias to make an alias
logcat Show the log from the device
p4aRun p4a commands. Args must come after --, or use --alias
to make an alias
Copy after login

Buildozer pitfalls in the packaging process


If you encounter an error during the packaging process, you can modify the log_level in the buildozer.spec configuration file to 2, and then run it again. You can see the specific error information.


Error report: You might have missed to install 32bits libs


This error was reported when I ran it on centos7. The general idea is that the system is missing some 32bits. Dependency files.


Solution:

yum -y install --skip-broken glibc.i686 arts.i686 audiofile.i686 bzip2-libs.i686 cairo.i686 cyrus-sasl-lib.i686 dbus-libs.i686 directfb.i686 esound-libs.i686 fltk.i686 freeglut.i686 gtk2.i686 hal-libs.i686 imlib.i686 lcms-libs.i686 lesstif.i686 libacl.i686 libao.i686 libattr.i686 libcap.i686 libdrm.i686 libexif.i686 libgnomecanvas.i686 libICE.i686 libieee1284.i686 libsigc++20.i686 libSM.i686 libtool-ltdl.i686 libusb.i686 libwmf.i686 libwmf-lite.i686 libX11.i686 libXau.i686 libXaw.i686 libXcomposite.i686 libXdamage.i686 libXdmcp.i686 libXext.i686 libXfixes.i686 libxkbfile.i686 libxml2.i686 libXmu.i686 libXp.i686 libXpm.i686 libXScrnSaver.i686 libxslt.i686 libXt.i686 libXtst.i686 libXv.i686 libXxf86vm.i686 lzo.i686 mesa-libGL.i686 mesa-libGLU.i686 nas-libs.i686 nss_ldap.i686 cdk.i686 openldap.i686 pam.i686 popt.i686 pulseaudio-libs.i686 sane-backends-libs-gphoto2.i686 sane-backends-libs.i686 SDL.i686 svgalib.i686 unixODBC.i686 zlib.i686 compat-expat1.i686 compat-libstdc++-33.i686 openal-soft.i686 alsa-oss-libs.i686 redhat-lsb.i686 alsa-plugins-pulseaudio.i686 alsa-plugins-oss.i686 alsa-lib.i686 nspluginwrapper.i686 libXv.i686 libXScrnSaver.i686 qt.i686 qt-x11.i686 pulseaudio-libs.i686 pulseaudio-libs-glib2.i686 alsa-plugins-pulseaudio.i686 python-matplotli
Copy after login

Error report: Error compiling Cython file


The error is to the effect that there is an error in the cython file. It may be that the cython module is not installed. , or there is a problem with the version. Solution:

pip install cython==0.25
Copy after login

Error: IOError: [Errno 2] No such file or directory.... This is the error reported when copying the apk file to the project bin directory in the last step of packaging. It's a bug in buildozer.


Solution:


Modify the /usr/local/lib/python2.7/dist-packages/buildozer/tagets/android.py file:


Import at the beginning of the file:

from distutils.version import LooseVersion

将786行:XXX found how the apk name is really built from the title这一行以下的代码替换为:

__sdk_dir = self.android_sdk_dir
build_tools_versions = os.listdir(join(__sdk_dir, 'build-tools'))
build_tools_versions = sorted(build_tools_versions, key=LooseVersion)
build_tools_version = build_tools_versions[-1]
gradle_files = ["build.gradle", "gradle", "gradlew"]
is_gradle_build = any((exists(join(dist_dir, x)) for x in gradle_files)) and build_tools_version >= ’25.0'
Copy after login

buildozer虚拟机

kivy官方推出了一个buildozer虚拟机镜像,已经安装好了buildozer以及一些依赖文件,为buildozer打包测试提供平台。由于之前我在mac上利用buildozer打包一直报错,后来换成centos也依然没有成功,因此便下载了此虚拟机,测试效果如下:

How to use Python to develop apps

说明:对于无法解决依赖问题的朋友,可以使用此虚拟机进行程序打包,开发环境还是推荐用自己的本机。

The above is the detailed content of How to use Python to develop apps. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

See all articles