How to import modules into Brython

1,373 views
Skip to first unread message

Vitali Coltuclu

unread,
Dec 5, 2014, 1:38:40 PM12/5/14
to bry...@googlegroups.com

Hello Everyone,

I am trying to import into Brython a module (pubchempy) that I installed on my Linux system using pip. I receive the following errors:

"Error 404 means that Python module pubchempy was not found at url http://127.0.0.1:5000/static/js/brython/Lib/site-packages/pubchempy.py" brython.js:7264
"Error 404 means that Python module pubchempy was not found at url http://127.0.0.1:5000/static/js/brython/Lib/site-packages/pubchempy/__init__.py" brython.js:7264

"Error 404 means that Python module __future__ was not found at url http://127.0.0.1:5000/static/js/brython/Lib/site-packages/__future__.py" brython.js:7264
"Error 404 means that Python module __future__ was not found at url http://127.0.0.1:5000/static/js/brython/Lib/site-packages/__future__/__init__.py" brython.js:7264

The actual list of "module was not found" is long. I just included first two samples. Then, I placed pubchempy.py and __future__.py modules in static/js/brython/Lib/site-packages folder and the errors above are gone. So I am wondering if there is a simpler way of importing modules into Brython.

Thanks for your time,
Vitali

Christophe Bal

unread,
Dec 5, 2014, 2:01:18 PM12/5/14
to bry...@googlegroups.com
Hello.

How do you do that ?


Christophe BAL
Enseignant de mathématiques en Lycée et développeur Python amateur
---
French math teacher in a "Lycée" and amateur developer in Python

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/6717bd1d-2c3c-4e03-9820-c83cfcdf67d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Olemis Lang

unread,
Dec 5, 2014, 2:05:47 PM12/5/14
to brython


On Fri, Dec 5, 2014 at 1:38 PM, Vitali Coltuclu <rus...@gmail.com> wrote:

Hello Everyone,


Hi !
:)
 
I am trying to import into Brython a module (pubchempy) that I installed on my Linux system using pip. I receive the following errors:


The attempt is made to load your module by looking up source code in Brython site-packages folder , hence not found ...


The actual list of "module was not found" is long. I just included first two samples. Then, I placed pubchempy.py and __future__.py modules in static/js/brython/Lib/site-packages folder and the errors above are gone. So I am wondering if there is a simpler way of importing modules into Brython.



This is a very common question in the forums . The answer is to do something like this 

{{{#!html

<body onload="brython({debug:1, pythonpath:['src/python']})">
}}}

Provided that your module may be downloaded from src/python sub-path by issuing HTTP GET requests .

--
Regards,

Olemis - @olemislc

Apache™ Bloodhound contributor
http://issues.apache.org/bloodhound
http://blood-hound.net

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:


Vitali Coltuclu

unread,
Dec 5, 2014, 2:13:46 PM12/5/14
to bry...@googlegroups.com
Hello Christophe,

Do you mean how I import the module? I have a main.py file which is loaded by Brython. Here are my imports:

from browser import document as doc, alert, window
from browser import ajax
import pubchempy as pcp
import json

Thanks,
Vitali

--
You received this message because you are subscribed to a topic in the Google Groups "brython" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/brython/IzNg0cSk6kI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to brython+u...@googlegroups.com.

To post to this group, send email to bry...@googlegroups.com.

André

unread,
Dec 5, 2014, 2:57:26 PM12/5/14
to bry...@googlegroups.com


On Friday, 5 December 2014 14:38:40 UTC-4, Vitali Coltuclu wrote:
 
The actual list of "module was not found" is long. I just included first two samples. Then, I placed pubchempy.py and __future__.py modules in static/js/brython/Lib/site-packages folder and the errors above are gone. So I am wondering if there is a simpler way of importing modules into Brython.

Like any modules, on any computers, to import a module using Python, that module needs to be in a known path (listed in sys.path). When you install a module on your computer using pip, pip puts that module in a standard location in your Python distribution.  Brython is a separate implementation of Python and it is located in a completely different location.  At this moment, there does not exist an equivalent to pip (bip?) for Brython.  So, you must move or copy the files by yourself in a location known by Brython as a place from where it can import files.   This is what you did (correctly) when you put them in static/js/brython/Lib/site-packages.  You'll have to do the same for all other modules which you intend to import from Brython.

André

Kiko

unread,
Dec 6, 2014, 3:55:48 AM12/6/14
to bry...@googlegroups.com
To complete what André said:

Not all python modules are directly available in Python. If a regular
CPython package has several dependencies you should check that the
dependencies are available in Brython. In this case, __future__
doesn't exist in Brython as it is focused in Python3. Also, the
logging module, needed by pubchempy, is not still available in
Brython.

If a CPython module has C, C++, fortran,..., dependencies it will not
run in Brython. Brython runs in a different environment, Browser, than
CPython, where javascript is the only first class citizen.
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To post to this group, send email to bry...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/f3ad5d69-025f-4432-ae91-58c105df96a0%40googlegroups.com.

Billy Earney

unread,
Dec 6, 2014, 8:17:44 AM12/6/14
to bry...@googlegroups.com

Also, those modules do not currently run in brython.  One day they might, if the c code is translated to python or javascript.

Vitali Coltuclu

unread,
Dec 6, 2014, 9:23:20 AM12/6/14
to bry...@googlegroups.com

Thank you all for a detailed explanation on how importing modules works. It would be nice to have such functionality in Brython.

Best,
Vitali
Reply all
Reply to author
Forward
0 new messages