0

我正在尝试将 python 集成到我的 android java 项目中,并对 Gradle 文件进行以下更改。但它会产生模块错误。但是我已经给 pip 安装列表提供了相同的模块。它安装成功,但仍然显示相同的错误。

请参阅以下错误:java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}:com.chaquo.python.PyException:ModuleNotFoundError:没有名为“quaternion”的模块

应用程序级别:

 plugins {
    id 'com.android.application'
    id 'com.chaquo.python'

}
android {
    compileSdkVersion 29
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        python {
            buildPython "C:/Users/Bridge Global/AppData/Local/Programs/Python/Python38/python.exe"

            pip {
                // A pip requirement specifier, with or without a version number:

                install "scipy"
                install "numpy"
                install "quaternion"
                install "pandas"
                install "matplotlib"

            }


        }
        ndk {
            abiFilters "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }

            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
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

项目级等级:

buildscript {
    repositories {
        google()
        jcenter()
        maven { url "https://chaquo.com/maven" }    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.0"
        classpath "com.chaquo.python:gradle:8.0.0"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

主要活动

if (! Python.isStarted()) {
   
    Python.start(new AndroidPlatform(this));
    Python py = Python.getInstance();
    PyObject pyObject =py.getModule("backend");
}

任何帮助将不胜感激。

4

1 回答 1

0

文档中所示,模块名称Quaternion带有大写 Q。大小写与 pip 无关,但在import语句中很重要。

或者您可能打算安装 pip 包numpy-quaternion,其 Python 模块名称quaternion带有小写 q。不幸的是,这是一个本机包,目前不适用于 Chaquopy,但还有一些其他纯 Python 替代方案可能对您有用:

于 2020-10-27T11:36:29.133 回答