Convert python into javascript

211 views
Skip to first unread message

fira...@gmail.com

unread,
Nov 15, 2013, 10:55:49 AM11/15/13
to emscripte...@googlegroups.com
Hey!

I have been trying to convert a simple python script into javascript for quite some time now.

I start by creating a basic script:
print 'Hello from python'

I then run cython to convert the python script into C:
cython --embed test.pyx

Finally I run emcc on the generated C file:
emcc test.c libpython2.7.so -IInclude -I. 

When I run the generated javascript in my browser I receive the following errors:
preload time: 2 ms a.out.js:99
Could not find platform independent libraries <prefix> a.out.js:99
Could not find platform dependent libraries <exec_prefix> a.out.js:99
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] a.out.js:99
ImportError: No module named site a.out.js:99
exit(1) called a.out.js:96 

This basically tells me python was unable to load site.py. Fine, I tried compiling with emcc again adding --embed-file site.py, but to no avail. I also tried to add import site.py in my python script file, did not work either. I have attempted several options here, such as using the -s LINKABLE=1 flag to see if perhaps parts of my code is being optimized away. This does not seem to be the case. 

Worth mentioning is that I have prior to this already compiled the libpython2.7.so file according to the guide over at github.

Any help would be very much appreciated.

Alon Zakai

unread,
Nov 15, 2013, 8:58:01 PM11/15/13
to emscripte...@googlegroups.com
Have you verified this works in a native build? (The whole cython etc. stuff sounds fairly complex.)

- Alon


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

fira...@gmail.com

unread,
Nov 15, 2013, 9:41:05 PM11/15/13
to emscripte...@googlegroups.com
Do you mean if I tried making it work using gcc or another compiler? If so yes. I have compiled the cython generated file using gcc and received the correct result.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

Alon Zakai

unread,
Nov 15, 2013, 10:33:22 PM11/15/13
to emscripte...@googlegroups.com
Then I guess you need to debug into the python code. I would start by asking the python people how python knows to load site.py, then you can narrow down what to debug.

- Alon



To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

Ryan Kelly

unread,
Nov 17, 2013, 6:23:44 PM11/17/13
to emscripte...@googlegroups.com
On 16/11/2013 2:55 AM, fira...@gmail.com wrote:
> Finally I run emcc on the generated C file:
>
> emcc test.c libpython2.7.so -IInclude -I.
>
>
> When I run the generated javascript in my browser I receive the
> following errors:
>
> preload time: 2 ms a.out.js:99
> Could not find platform independent libraries <prefix> a.out.js:99
> Could not find platform dependent libraries <exec_prefix> a.out.js:99
> Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] a.out.js:99
> ImportError: No module named site a.out.js:99
> exit(1) called a.out.js:96
>
>
> This basically tells me python was unable to load site.py.

Python can be quite picky about the layout of files it expects to find
on disk. There are two things you could try here.

The first is to disable the loading of site.py. For a command-line
python this is done with the "-S" command-line option, I'm not sure if
that will also work with the embedding thing you're doing here.

However, I suspect it will still try to load other modules at startup
e.g the "os" module.

The second option is to embed the entire python stdlib into your
javascript file, at the same location as it is on your local machine.
For example:

--embed-file /usr/lib/python2.7@usr/lib/python2.7

(I've found this "path@path" syntax necessary to prevent complaints
about embedding files with absolute paths; your mileage may vary)

This will embed a *lot* of stuff, but it should let python find the
files it wants.

If you manage to get it working with the whole stdlib included, then you
can gradually try including fewer and fewer files until you find the
minimal set that it needs. You'll probably wind up with at least:

--embed-file /usr/lib/python2.7/site.py@usr/lib/python2.7/site.py
--embed-file /usr/lib/python2.7/os.py@usr/lib/python2.7/os.py


As suggested in the original error message, you can also use the
PYTHONHOME environment variable to control where python looks for these
files. More details on how to tweak the python startup sequence are
available at:

http://docs.python.org/2/using/cmdline.html


Hope this helps!


Cheers,

Ryan

Dan Stromberg

unread,
Nov 18, 2013, 6:35:12 PM11/18/13
to emscripte...@googlegroups.com
On Fri, Nov 15, 2013 at 7:55 AM, <fira...@gmail.com> wrote:
Hey!

I have been trying to convert a simple python script into javascript for quite some time now.

Depending on what you need your script to do, there might be an easier way:
http://stromberg.dnsalias.org/~dstromberg/pybrowser/python-browser.html

The URL summarizes Python-on-JavaScript implementations.

Reply all
Reply to author
Forward
0 new messages