In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use Python to develop App". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Recently thought of trying to use python to develop an app,google search, found that there is indeed a way to find, there are also some relatively mature modules, so they began to hands-on actual combat, in the process found that there are a lot of holes, but finally rely on google to solve, so take notes.
Say what you said before.
Although the python language is very omnipotent, but using it to develop app still seems a bit wrong, so the app developed with python should be used as coding practice, or for self-entertainment, plus the current module in this area is not particularly mature, bug is more, all in all, you are advised not to enter lightly.
Preparatory work
Using python to develop app requires a module of python-kivy,kivy is an open source, cross-platform Python development framework for developing innovative applications. In short, this is a python desktop program development framework (similar to wxpython modules). The powerful thing is that kivy supports linux, mac, windows, android, ios platforms, which is why the development of app needs this module.
Although kivy is cross-platform, if you want to use python code on different platforms, you also need to package the python code into executable programs for the corresponding platform. Fortunately, there is a packaging tool project under the kivy project-buildozer, which is an 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, which will not be introduced here.
Build kivy development environment
The kivy development environment needs to be installed on pc. Here is a demonstration of the installation process under mac and linux.
Install kivy for mac
Install some dependency packages:
Brew install pkg-config sdl2 sdl2_image sdl2_ttf sdl2_mixer gstreamer
Install cython and kivy:
Pip install cython==0.25pip install kivy
If the installation of kivy reports an error, install kivy using the following method:
Git clone https://github.com/kivy/kivypython setup.py install
Post-installation testing:
$pythonPython 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 darwinType "help", "copyright", "credits" or "license" for more information. > import kivy [INFO] [Logger] Record log in / Users/didi/.kivy/logs/kivy_18-05-084 txt [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)]
Note: if the import kivy module does not report an error, it means that the installation is successful.
Install kivy for centos7
Install dependencies first:
Yum install\ make\ mercurial\ automake\ gcc\ gcc-c++\ SDL_ttf-devel\ SDL_mixer-devel\ khrplatform-devel\ mesa-libGLES\ gstreamer-plugins-good\ gstreamer\ gstreamer-python\ mtdev-devel\ python-devel\ python-pip\ java-devel
Install cython and kivy:
Pip install Cython==0.20pip install kivy
Centos installation kivy reference: https://kivy.org/docs/installation/installation-linux.html#using-software-packages
Description: other ways to install kivy can be moved: https://kivy.org/#download (need to climb over the wall)
Develop the first python app with kivy
After installing kivy, you can develop app programs. Here we demonstrate the hello-world program. The more complex use of kivy is not the focus of this article, which will be described later.
1) create a main.py file and write:
#!-*-coding:utf-8-*-from kivy.app import Appclass HelloApp (App): passif _ _ name__ = ='_ main__':HelloApp () .run ()
2) create a hello.kv file and write:
Label:text: 'Hello, World! I am nMask'
Simple description: main.py is an entry function, defines a HelloApp class, this class inherits the kivy.app;hello.kv file is kivy procedures, equivalent to the definition of interface style, etc., the file naming rules for the lowercase class name and remove app.
Run the first python apppython main.py
Running result:
Install buildozer tools
With the above coding, I created my first python app program, which can run directly on mac, linux, and windows platforms, so how do I make it run on Android or iPhone? We know that to run on Android, we need to package it as an apk installer, so we need to use the previously mentioned buildozer tool (buildozer tool can package kivy programs, support android, ios, etc.). The installation process of buildozer is relatively simple:
Pip install buildozer uses buildozer tools to package kivy programs into apk
Run under the python project directory:
Buildozer init
Running successfully will create a configuration file buildozer.spec. You can change the name of app by modifying the configuration file, etc., and then run:
Buildozer android debug deploy run
Running the above command will generate a cross-platform installation package that can be used for Android, ios, etc., if used for Android, using the python-for-android project.
When you run the above command for the first time, you will automatically download necessary files such as Android sdk on the system, as shown in the figure below. (the process requires climbing over the wall, and there are a lot of dependencies to download)
Note: here is only a demonstration packaged into apk files, iso platform can be self-research, reference document: https://github.com/kivy/buildozer.
Python apk program testing
If all the above steps are successful, an apk file should be generated in the bin directory under the project directory, similar to the following:
Then download the apk to an Android phone and install it. The test results are as follows:
Open app
Buildozer instructions Usage:buildozer [--profile] [--verbose] [target]... buildozer-- versionAvailable targets:android Android target, based on python-for-android projectios iOS target, based on kivy-ios projectandroid_old Android target Based on python-for-android project (old toolchain) Global commands (without target): distclean Clean the whole Buildozer environment.help Show the Buildozer help.init Create an initial buildozer.spec in the current directoryserve Serve the bin directory via SimpleHTTPServersetdefault Set the default command to run when no arguments are givenversion Show the Buildozer versionTarget commands:clean Clean the target environmentupdate Update the target dependenciesdebug Build the application in debug moderelease Build the application in release modedeploy Deploy the application on the devicerun Run the application on the deviceserve Serve the bin directory via SimpleHTTPServerTarget "android_old" commands:adb Run adb from the Android SDK. Args must come after -, oruse-- alias to make an aliaslogcat Show the log from the deviceTarget "ios" commands:list_identities List the available identities to use for signing.xcode Open the xcode project.Target "android" commands:adb Run adb from the Android SDK. Args must come after -, oruse-- alias to make an aliaslogcat Show the log from the devicep4a Run p4a commands. Args must come after -, or use-- aliasto make an aliasbuildozer potholes in the packing process
If you encounter an error during the packaging process, you can change the log_level in the buildozer.spec configuration file to 2, and then run it again to see the specific error message.
Error report: You might have missed to install 32bits libs
This is my fault of running the Times on centos7, to the effect that the system lacks some 32-bit dependent 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
Reference: https://ask.fedoraproject.org/en/question/9556/how-do-i-install-32bit-libraries-on-a-64-bit-fedora/
Error report: Error compiling Cython file
The main idea of the error is that there is an error in the cython file, maybe the cython module is not installed, or there is a problem with the version.
Solution:
Pip install cython==0.25 error: IOError: [Errno 2] No such file or directory... ..
This is the last step in the package, copy the apk file to the project bin directory under the Times's fault, is a bug of buildozer.
Solution:
Modify the / usr/local/lib/python2.7/dist-packages/buildozer/tagets/android.py file:
(1) Import at the beginning of the file:
From distutils.version import LooseVersion
(2) replace line 786: XXX found how the apk name is really built from the title with the following line:
_ _ sdk_dir = self.android_sdk_dirbuild_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'buildozer virtual machine
Kivy has officially launched a buildozer virtual machine image, which has installed buildozer and some dependent files to provide a platform for buildozer packaging and testing. Since I have been reporting errors in buildozer packaging on mac, and then I still failed to change it to centos, I downloaded this virtual machine and the test results are as follows:
Virtual machine download address: http://txzone.net/files/torrents/kivy-buildozer-vm-2.0.zip
Note: for friends who can not solve the dependency problem, you can use this virtual machine for program packaging, the development environment is still recommended to use their own local machine.
"how to use Python to develop App" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.