【问题标题】:Running python script with chaquopy in android studio在 android studio 中使用 chaquopy 运行 python 脚本
【发布时间】:2021-07-20 15:09:04
【问题描述】:

所以我是 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 android-studio kotlin chaquopy


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-03
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 2021-09-08
      • 2021-06-11
      相关资源
      最近更新 更多