简体   繁体   English

Python 运行 JavaScript 3.8

[英]Python run JavaScript 3.8

I have node.js installed and want to know if there is any way to run js code on python 3.8 .我已经安装了 node.js,想知道是否有任何方法可以在python 3.8上运行 js 代码。 I need to use a large piece of code (babylon) for my web application.我需要为我的 Web 应用程序使用一大段代码 (babylon)。 Is there any easy way to do this?有什么简单的方法可以做到这一点吗?

Running Code运行代码

I admit I have not messed with this before, but I think the best way to go about it is to have a js file that you can run from python.我承认我以前没有搞过这个,但我认为最好的方法是拥有一个可以从 python 运行的 js 文件。 I found 2 ways to accomplish this- one that returns output and one that simply just runs it.我找到了两种方法来实现这一点——一种返回输出,另一种只是运行它。

Getting Output获取输出

You may be able to use the subprocess module to run the node and record it's output for later use.您可以使用subprocess模块来运行节点并记录它的输出以供以后使用。

import subprocess
myValue = subprocess.check_output(['node','index.js'])

Python subprocess.check_output Documentation Python subprocess.check_output文档

No Output无输出

If you do not require to read the output of the file, you can just run it with the os module.如果您不需要读取文件的输出,则可以使用os模块运行它。

import os
os.system("node index.js")

Python os.system documentation Python os.system文档

I do like the answer given.我喜欢给出的答案。 I would like to add that one could use the Naked library:我想补充一点,可以使用 Naked 库:

from Naked.toolshed.shell import execute_js
execute_js('file.js')

One can read more here: https://naked.readthedocs.io/可以在此处阅读更多信息: https : //naked.readthedocs.io/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM