使用Buildozer打包Kivy时出现Python2.7调用问题咨询
Let's break down your problem step by step:
First, why is Python 2.7 being called?
There are a few common reasons for this discrepancy, even when you intended to use Python 3:
- Your system's default
pythoncommand points to Python 2.7. Many Linux distributions (including the Kivy-provided VM) still default to Python 2.7 for the genericpythonalias. If Buildozer or its helper scripts usepythoninstead of explicitly callingpython3, it'll fall back to the system's pre-installed Python 2.7. - You might have installed Buildozer using Python 2's
pipinstead of Python 3'spip3. This links Buildozer's executable to the Python 2 environment, overriding your intention to use Python 3. - Your VM's environment variables aren't configured to prioritize Python 3. Without setting
python3as the default alias, tools will default to the system's older Python version.
Is this the root cause of your build failure?
Absolutely. Your buildozer.spec specifies python3crystax as a requirement, which relies entirely on a Python 3 runtime. When the build process uses Python 2.7 to execute scripts designed for Python 3, you'll hit compatibility issues—like syntax errors, missing Python 3-specific modules, or mismatched dependencies. The traceback showing /usr/lib/python2.7/runpy.py confirms the wrong Python version is driving the build, and this is almost certainly why your build fails with sh.ErrorReturnCode_2.
How to fix it
Here's a step-by-step fix to force Buildozer to use Python 3:
Verify your Python versions
Run these commands in your VM terminal to confirm which versions are active:python --version python3 --versionYou'll likely see Python 2.7 for the first command, and a Python 3.x release for the second.
Reinstall Buildozer using Python 3
- First, remove any existing Buildozer installations:
pip uninstall buildozer -y pip3 uninstall buildozer -y - Install Buildozer using Python 3's package manager:
pip3 install buildozer - If you installed from source, ensure you use Python 3 to run the setup:
cd path/to/buildozer-source python3 setup.py install
- First, remove any existing Buildozer installations:
Update your buildozer.spec to enforce Python 3
Add or confirm this line is present in your spec file:android.python_version = 3This explicitly tells Buildozer to target Python 3 for the Android build.
Set Python 3 as your default alias
Edit your~/.bashrcfile to add:alias python=python3Apply the change immediately:
source ~/.bashrcClean and rebuild
Clear cached build files to eliminate any leftover Python 2.7 artifacts:buildozer android cleanThen restart the build process:
buildozer android debug
As an extra check, confirm your android.ndk_path in buildozer.spec points to the correct Crystax NDK location—an incorrect path could cause additional issues, but the Python version mismatch is your primary blocker right now.
内容的提问来源于stack exchange,提问作者jasan

