如何解决Vue.js项目(CLI)中Pythonshell的“child_process_1.spawn不是函数”问题?

2024-09-29 22:31:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在Vue.js CLI项目中运行以下代码,该项目使用python shell(节点模块)执行python文件:

methods: {
      launchPy() {
          let {PythonShell} = require('python-shell')
          PythonShell.run('my_script.py', null, function (err) {
            if (err) throw err;
              console.log('finished2');
          });
},

在“my_script.py”中,插入了以下代码:

f = open("demo.txt", "a")
f.write("Writing a file")
f.close()

当我运行localhost并通过botton click:launchPy()执行该方法时,会出现下一个错误:

[Vue warn]: Error in v-on handler: "TypeError: child_process_1.spawn is not a function"

我已经安装了node.js和pythonshell。即使我在一个单独的fileName.js文件中运行完全相同的函数launchPy()代码并执行'node fileName.js'命令,它也会平稳运行,实际上,python文件也会执行

我如何解决这个问题


Tags: 文件项目代码pynodemyjsscript

热门问题