Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling trivial python program to javascript using cython and emscripten on mac

I am trying to generate javascript from python using cython and emscripten.

hello.py:

print 'Hello world.'

Then I compile this to c using cython

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

This generates a hello.c file which I compile with

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

This works for gcc or clang. When I execute ./a.out I get the expected output

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

next I want to compile hello.c to javascript using emscripten

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

I get

>>> 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++'

It still generates a a.out.js file which I try to run in node.js

>>> node a.out.js

I get a reference error

>>> ReferenceError: _Py_SetProgramName is not defined

I tried changing the generated javscript a little bit, but basically I think all the _Py_ functions are not defined.

Does anyone have any experience with this, or any suggested fixes?

like image 577
Eoin Murray Avatar asked Jun 12 '13 11:06

Eoin Murray


1 Answers

You will need to compile the embedabble python library -lpython2.7 to javacsript too so that it is available for your javacsript program.

Thankfully, the work to do this has already been done in empythoned. Which provides an embedded python compiled to Javascript.

You should be able to use empythoned to provide the missing _Py_SetProgramName

like image 112
brice Avatar answered Sep 30 '22 08:09

brice