程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 android studio 中使用 chaquopy 运行 python 脚本大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 android studio 中使用 chaquopy 运行 python 脚本?

开发过程中遇到在 android studio 中使用 chaquopy 运行 python 脚本的问题如何解决?下面主要结合日常开发的经验,给出你关于在 android studio 中使用 chaquopy 运行 python 脚本的解决方法建议,希望对你解决在 android studio 中使用 chaquopy 运行 python 脚本有所启发或帮助;

所以我是 androID studio 的新手,现在我正在编写我的第一个应用程序,最近遇到了一个我无法解决的问题。

我一直在尝试使用 chaquopy 来调用我在 python 中编写的函数,但是每次我尝试运行调用 python 代码的活动时,我都会收到此错误: com.chaquo.python.PyException: ModuleNotFoundError: 没有名为“脚本”的模块。 我的python文件的名字是script.py

这是我为调用python代码的部分编写的代码:

    overrIDe fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceStatE)
    setContentVIEw(R.layout.activity_alarm_pagE)

    if (!Python.isstarted())
        Python.start(Androidplatform(this))
    val py: Python = Python.geTinstance()
    val pyobj: PyObject = py.getModule("script")

    date_choose()
    val search_btn: button = findVIEwByID<button>(R.ID.search)
    search_btn.setonClickListener(VIEw.onClickListener {
        fun OnClick(vIEw : VIEw)
        {
            val obj : PyObject = pyobj.callAttr("SportsDemo",game_day,game_month,game_year)
            val diff_games: TextVIEw = findVIEwByID<TextVIEw>(R.ID.diff_games)
            diff_games.text = obj.toString()
        }


    })

如果有人知道我可以做些什么来解决这个问题,如果他们帮助我,我会很高兴。 谢谢。

解决方法

检查 Python 文件的位置。来自Chaquopy documentation:

默认情况下,Chaquopy 会在每个 source set 的 python 子目录中查找 Python 源代码。例如,主源集的 Python 代码应该放在 src/main/python 中。

大佬总结

以上是大佬教程为你收集整理的在 android studio 中使用 chaquopy 运行 python 脚本全部内容,希望文章能够帮你解决在 android studio 中使用 chaquopy 运行 python 脚本所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。