You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeError: function()参数'code'必须为code而非str类型的报错排查求助

TypeError: function() argument 'code' must be code, not str when using python_weather with Buildozer

Let's break down how to diagnose and fix this issue—this error typically pops up due to dependency version conflicts or quirks in the Buildozer Android packaging process, rather than your core code logic (though we'll fix a couple of small code issues too).

First, Let's Rule Out Code Syntax/Logic Issues

Your code has two minor problems that aren't the direct cause of the TypeError, but fixing them will eliminate other potential headaches:

  1. Broken conditional check: if len(d2)!=0 or len(d2)!=1: will always evaluate to True (any string's length is either not 0 or not 1). Replace it with:
    if not (len(d2) == 0 or len(d2) == 1):
    
  2. Missing closing parenthesis: The f.write() line is missing a closing ) at the end, plus adding a newline will keep your output readable. Adjust it to:
    f.write(f"{forecast.date} {forecast.sky_text}\n")
    

Now, Fix the Core TypeError

This error stems from issues with how Buildozer packages asyncio/aiohttp dependencies for Android. Here's how to resolve it:

1. Pin Compatible Dependency Versions

Old or mismatched versions of aiohttp (a dependency of python_weather) often cause bytecode-related errors during Android packaging. Update your buildozer.spec to use stable, compatible versions:

# In buildozer.spec
requirements = python3, python_weather, aiohttp==3.8.6, async_timeout==4.0.2

These versions are known to work reliably with Buildozer and Python 3.8+.

2. Adjust Buildozer Configuration

  • Ensure Network Permissions: Since your app fetches weather data, add internet access to your buildozer.spec:
    android.permissions = INTERNET
    
  • Rebuild Without Cache: Old cached build files can cause weird issues. Run a clean build:
    buildozer android clean
    buildozer android debug deploy run
    

3. Verify Local Execution First

Test your code locally (on Windows/Linux) before repackaging. If it runs fine there, the problem is definitely with the Android packaging process. If it fails locally, reinstall dependencies with the pinned versions:

pip uninstall -y python_weather aiohttp async_timeout
pip install python_weather aiohttp==3.8.6 async_timeout==4.0.2

4. Additional Troubleshooting (If Issues Persist)

If you still see the error, try adding this to your buildozer.spec to force proper bytecode handling:

android.additional_build_args = --no-compile-pyo

This disables compiling .pyo files, which can sometimes resolve code/str mismatches during packaging.

Final Corrected Code

Here's your full code with the fixes applied:

with open("city.txt","r") as f:
    d2 = f.read().strip()

# Fix conditional logic
if not (len(d2) == 0 or len(d2) == 1):
    m = d2
else:
    m = "new york"

import asyncio
import python_weather

async def getweather():
    client = python_weather.Client(format=python_weather.IMPERIAL)
    weather = await client.find(m)
    for forecast in weather.forecasts:
        with open("wx1.txt","a+") as f:
            # Fix missing parenthesis and add newline
            f.write(f"{forecast.date} {forecast.sky_text}\n")
    await client.close()

# Execute the async function
asyncio.run(getweather())

内容的提问来源于stack exchange,提问作者Mohit Narwani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 00:39:08