我正在尝试编译一个用 python 和 kivy (kivyMD) 和 openAI 开发的 android apk 应用程序。编译成功,应用程序运行,但调用 openai.ChatCompletion 函数最终导致错误导致应用程序崩溃: IsADirectoryError: [Errno 21] 是一个目录:'/data/data/org.wiseplat.chattranslator/files/app'
当尝试在第 196 行打开可执行文件时,“/python3/Lib/platform.py”的 libc_ver 函数中会出现此错误:(with open(executable, 'rb'),这显然是已编译应用程序中的一个目录。请告知可能是什么问题以及如何解决?
版本python==3.9.9,openai==0.27.2 使用 buildozer 构建的 apk。
来自文件“/project/.buildozer/android/app/main.py”的第 209 行,第 209 行,在 send_message 中:
response = openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=[{"role": "user", "content": response_text},])
来自“/python3/Lib/platform.py”的函数 libc_ver:
def libc_ver(executable=None, lib='', version='', chunksize=16384):
""" Tries to determine the libc version that the file executable
(which defaults to the Python interpreter) is linked against.
Returns a tuple of strings (lib,version) which default to the
given parameters in case the lookup fails.
Note that the function has intimate knowledge of how different
libc versions add symbols to the executable and thus is probably
only useable for executables compiled using gcc.
The file is read and scanned in chunks of chunksize bytes.
"""
if not executable:
try:
ver = os.confstr('CS_GNU_LIBC_VERSION')
# parse 'glibc 2.28' as ('glibc', '2.28')
parts = ver.split(maxsplit=1)
if len(parts) == 2:
return tuple(parts)
except (AttributeError, ValueError, OSError):
# os.confstr() or CS_GNU_LIBC_VERSION value not available
pass
executable = sys.executable
V = _comparable_version
if hasattr(os.path, 'realpath'):
# Python 2.2 introduced os.path.realpath(); it is used
# here to work around problems with Cygwin not being
# able to open symlinks for reading
executable = os.path.realpath(executable)
with open(executable, 'rb') as f: #(line 196 from python3/Lib/platform.py)
binary = f.read(chunksize)
pos = 0
while pos < len(binary):
if b'libc' in binary or b'GLIBC' in binary:
m = _libc_search.search(binary, pos)
else:
m = None
if not m or m.end() == len(binary):
chunk = f.read(chunksize)
if chunk:
binary = binary[max(pos, len(binary) - 1000):] + chunk
pos = 0
continue
if not m:
break
libcinit, glibc, glibcversion, so, threads, soversion = [
s.decode('latin1') if s is not None else s
for s in m.groups()]
if libcinit and not lib:
lib = 'libc'
elif glibc:
if lib != 'glibc':
lib = 'glibc'
version = glibcversion
elif V(glibcversion) > V(version):
version = glibcversion
elif so:
if lib != 'glibc':
lib = 'libc'
if soversion and (not version or V(soversion) > V(version)):
version = soversion
if threads and version[-len(threads):] != threads:
version = version + threads
pos = m.end()
return lib, version
在windows上应用程序正常运行,在apk中编译后出现错误。
adb logcut 的输出:
03-20 01:45:48.060 2805 2928 I python:文件“/project/.buildozer/android/app/main.py”,第 209 行,在 send_message 中 03-20 01:45:48.061 2805 2928 I python:文件“/project/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/chattranslator/armeabi-v7a/openai/api_resources/chat_completion.py ",第 25 行,在创建中
03-20 01:45:48.062 2805 2928 I python:文件“/project/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/chattranslator/armeabi-v7a/openai/api_resources/abstract/ engine_api_resource.py”,第 153 行,在创建中
03-20 01:45:48.063 2805 2928 I python:文件“/project/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/chattranslator/armeabi-v7a/openai/api_requestor.py” ,第 216 行,应要求
03-20 01:45:48.064 2805 2928 I python: 文件“/project/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/chattranslator/armeabi-v7a/openai/api_requestor.py” ,第 509 行,在 request_raw
03-20 01:45:48.065 2805 2928 I python:文件“/project/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/chattranslator/armeabi-v7a/openai/api_requestor.py” , 第 490 行,在 _prepare_request_raw
03-20 01:45:48.066 2805 2928 I python:文件“/project/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/chattranslator/armeabi-v7a/openai/api_requestor.py” ,第 409 行,在 request_headers
03-20 01:45:48.068 2805 2928 I python:文件“/project/.buildozer/android/platform/build-armeabi-v7a/build/other_builds/python3/armeabi-v7a__ndk_target_21/python3/Lib/platform.py” ,第1235行,在平台
03-20 01:45:48.069 2805 2928 I python:文件“/project/.buildozer/android/platform/build-armeabi-v7a/build/other_builds/python3/armeabi-v7a__ndk_target_21/python3/Lib/platform.py” ,第 196 行,在 libc_ver 中
03-20 01:45:48.071 2805 2928 I python: IsADirectoryError: [Errno 21] 是一个目录:'/data/data/org.wiseplat.chattranslator/files/app'
03-20 01:45:48.071 2805 2928 I python:Python for android 结束。
更新:我解决了这个问题手动编辑 ua dict 从 request_headers openai 覆盖“平台”:从 platform.platform() 到 'Windows-10-10.0.19044-SP0'
哟,你能提供更多关于你做了什么来解决这个问题的细节吗?我有同样的。
谢谢, -安迪