V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
monkeycz
V2EX  ›  Node.js

python.js ,一个在 node.js 中调用 Python 代码的模块

  •  1
     
  •   monkeycz · Mar 20, 2015 · 4783 views
    This topic created in 4059 days ago, the information mentioned may be changed or developed.

    前一段时间用 node.js 做项目,感觉 node.js 在服务器方面游刃有余,但是其他方面难望 Python 之项背。于是撸了一个可以在 node.js 中调用 Python 代码的模块。支持 node.js ,io.js , nw.js 。

    例子如下:

    var python = require('python.js');
    var os = python.import('os');
    
    var path = require('path');
    
    assert(os.path.basename(os.getcwd()) == path.basename(process.cwd()));
    

    支持以异步模式调用 Python 的代码:

    def test():
        try:
            ## do samething
        except Exception as e:
            raise e
        return 'done'
    
    PYMODULE.test.async = true;
    PYMODULE.test(function (result, error) {
        if (!error)
            console.log(result);
    });
    

    最后放上项目地址:
    https://github.com/monkeycz/python.js

    3 replies    2015-03-21 14:59:39 +08:00
    ugmc
        1
    ugmc  
       Mar 20, 2015 via Android
    挺好
    gamexg
        2
    gamexg  
       Mar 21, 2015
    生产环境用起来怎么样?
    现在我 nodejs 调 python 是通过 rest.post(url) 来使用的。
    monkeycz
        3
    monkeycz  
    OP
       Mar 21, 2015
    @gamexg 目前手边有两个已发布的项目在用这个模块。项目里 node.js 和 Python 的代码比例都是接近 1:1,通过 python.js 进行桥接。暂时没发现什么问题。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2510 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 03:59 · PVG 11:59 · LAX 20:59 · JFK 23:59
    ♥ Do have faith in what you're doing.