javascript - 在 mac 上使用 cython 和 emscripten 将简单的 python 程序编译为 javascript

标签 javascript python node.js cython emscripten

我正在尝试使用 cython 和 emscripten 从 python 生成 javascript。

你好.py:

print 'Hello world.'

然后我用cython把它编译成c

>>> cython --embed hello.py -v

这会生成一个 hello.c 文件,我用它来编译

>>> gcc hello.c -I/usr/include/python2.7/ -lpython2.7

这适用于 gcc 或 clang。 当我执行 ./a.out 时,我得到了预期的输出

>>> ./a.out
>>> Hello world

接下来我想使用 emscripten 将 hello.c 编译成 javascript

>>> emcc hello.c -I/usr/include/python2.7/ -lpython2.7

我明白了

>>> WARNING  emcc: -I or -L of an absolute path encountered. 
>>> If this is to a local system header/library, it may cause problems 
>>> (local system files make sense for compiling natively on your system, 
>>> but not necessarily to JavaScript)
>>> clang: warning: argument unused during compilation: '-nostdinc++'

它仍然会生成一个 a.out.js 文件,我尝试在 node.js 中运行它

>>> node a.out.js

我得到一个引用错误

>>> ReferenceError: _Py_SetProgramName is not defined

我尝试稍微更改生成的 javscript,但基本上我认为所有 _Py_ 函数都未定义。

有没有人对此有任何经验或建议的修复方法?

最佳答案

您还需要将可嵌入的 python 库 -lpython2.7 编译为 javacsript,以便它可用于您的 javacsript 程序。

谢天谢地,这方面的工作已经在 empythoned 中完成了.它提供了一个编译为 Javascript 的嵌入式 python。

您应该能够使用 empythoned 来提供缺少的 _Py_SetProgramName

关于javascript - 在 mac 上使用 cython 和 emscripten 将简单的 python 程序编译为 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17064051/

相关文章:

javascript - AWS DynamoDB 未返回对 Lambda 函数的响应

python - 限制来自任何给定 IP 地址的请求数量

python - 将Python对象属性写入txt文件

python - 使用Python和SQLite创建表,没有这样的表

javascript - 销毁nodejs中对象的事件或回调

javascript - React-native全局变量未定义

javascript - 无法在javascript中调用同步功能

javascript - 如何使用 AJAX、计算字段、可排序列、粘贴等创建 SlickGrid?

javascript - 替换 JavaScript 中的引号

node.js - 无法从 API 上的 axios 接收数据