I am trying to generate javascript from python using cython and emscripten.
hello.py
:
print 'Hello world.'
Then I pile this to c using cython
>>> cython --embed hello.py -v
This generates a hello.c
file which I pile 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 pile 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 piling natively on your system,
>>> but not necessarily to JavaScript)
>>> clang: warning: argument unused during pilation: '-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?
I am trying to generate javascript from python using cython and emscripten.
hello.py
:
print 'Hello world.'
Then I pile this to c using cython
>>> cython --embed hello.py -v
This generates a hello.c
file which I pile 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 pile 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 piling natively on your system,
>>> but not necessarily to JavaScript)
>>> clang: warning: argument unused during pilation: '-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?
Share Improve this question asked Jun 12, 2013 at 11:15 Eoin MurrayEoin Murray 1,9553 gold badges23 silver badges34 bronze badges 9- Whoa. Python to Javascript via C? Would be interesting to know what your usecase is? – brice Commented Jun 12, 2013 at 11:21
- physics simulation written in python with numpy that I'd love get onto browsers. Also I'd love to write the vector manipulations with numpy and visualizations with Three.js, but thats very wishful thinking! – Eoin Murray Commented Jun 12, 2013 at 11:38
- Numpy will be very tough to port to javascript. It uses some native libraries written in FORTRAN. – brice Commented Jun 12, 2013 at 11:40
- Advice is to run the simulation on the server and update the visualisation via a websocket. I've done an identical thing in the past where WebGL was just a presentation frontend to a physics engine running on the server using python, sockJS and a tornado reactor as a server. Worked pretty well. – brice Commented Jun 12, 2013 at 11:43
- Perhaps, your probably right, thanks for the advice. – Eoin Murray Commented Jun 12, 2013 at 11:45
3 Answers
Reset to default 6You will need to pile 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 piled to Javascript.
You should be able to use empythoned to provide the missing _Py_SetProgramName
To make it works I think you need whole Python piled by emcc
to JavaScript to have proper libraries piled into code that node.js
can handle. Otherwise binary libraries you have remain intact. You cannot mix that.
In fact emcc
informs you about it with the warning if you read it carefully.
You need to find out how to cross-pile
Python into javascript prior to piling your own scripts. This is done already, because I saw it on repl.it.
Pyodide has finally (many years later) properly solved this problem. See https://pyodide/en/stable/ Many of the ponents of Pyodide are written partly using Cython.