我正在尝试用python编写一些代码,然后在android studio中运行,结果出现了这个错误。我不知道如何解决这个问题。
我的主要活动
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
if (!Python.isStarted()){
Python.start(AndroidPlatform(this))
}
val python=Python.getInstance()
val pyObject=python.getModule("myscript")
Column(modifier = Modifier.fillMaxSize()) {
var searchPromt by remember {
mutableStateOf("")
}
var result by remember {
mutableStateOf("")
}
TextField(value = searchPromt, onValueChange ={
searchPromt=it
})
Button(onClick = {
val obj=pyObject.callAttr("main",searchPromt)
result=obj.toString()
}) {
Text(text = "Generate")
}
Text(text = result)
}
}
}
}
我的身材,品位
android {
namespace 'com.example.textdetection'
compileSdk 33
defaultConfig {
applicationId "com.example.textdetection"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
ndk {
abiFilters "armeabi-v7a", "x86"
}
python {
buildPython "D:/setup/python/Python 2"
}
python{
pip{
install "transformers"
}
}
sourceSets {
main {
python.srcDir "src/main/python"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.2.0'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
我的Python文件
from transformers import pipeline
def main(prompt):
//Some code
我的日志
FATAL EXCEPTION: main
Process: com.example.textdetection, PID: 8647
com.chaquo.python.PyException: ModuleNotFoundError: No module named 'transformers'
生成错误
A problem occurred starting process 'command 'D:/setup/python/Python''. Please set buildPython to your Python executable path. See https://chaquo.com/chaquopy/doc/current/android.html#buildpython.
我知道这可能与文件路径和pip有关,但我不知道如何调用pip块,所以请帮助我。
1条答案
按热度按时间yc0p9oo01#
正如这里所说,
buildPython
应该是Python可执行文件的路径,而不是目录。