https://github.com/doloopwhile/PyExecJS
https://pypi.org/project/PyExecJS/
https://pypi.org/project/PyExecJS2/
PyExecJS只更新到1.5.0
PyExecJS2更新到 1.6.1
pip install PyExecJS
pip install PyExecJS2
PyExecJS有版本:
1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.1.0, 1.2.0, 1.3.0, 1.3.1, 1.4.0, 1.4.1, 1.5.0, 1.5.1
PyExecJS2有版本:
1.6.1
import execjs
ctx = execjs.compile('''
function add(a, b) {
return a + b;
}
''')
current_path = os.getcwd() # 假设当前工作目录为
parent_directory_path = os.path.dirname(current_path) # 获取上一层目录
print(current_path)
print(parent_directory_path)
## 看一下现在的路径
with open('script.js', 'r') as file:
script = file.read()
ctx = execjs.compile(script)
自带方法 - open() - 读写文件打开文件详细可以看这篇
ctx = execjs.compile('''
var jQuery = require('jquery');
''')
result = ctx.call('add', 2, 3)
print(result) # 输出:5
ctx = execjs.compile('''
var person = {
name: 'John',
age: 30
};
person.age;
''')
result = ctx.eval('person')
result = ctx.eval('person.age')