Python3用PyExecJS运行JS

1、安装NodeJS
sudo curl -sL https://rpm.nodesource.com/setup_10.x | bash -
sudo yum install -y nodejs

2、安装PyExecJS
python3 -m pip install PyExecJS

3、举个引入栗子

一个JS文件

// a.js
function hello(name){
  return "Hello, "+name+"."
}

调用JS文件中的函数

# a.py
import execjs
def say(name):
    with open("a.js", 'r', encoding='UTF-8') as fp:
        ajs = fp.read()
    loader = execjs.compile(ajs)
    r = loader.call('hello', name)
    return r

if __name__ == "__main__":
    print(say("geek"))

4、参考资料
https://pypi.org/project/PyExecJS
https://github.com/nodesource/distributions

你可能感兴趣的:(Python3用PyExecJS运行JS)