简体   繁体   中英

How to import python library from url in Brython

I have this code:

<script type="text/python" src="https://cdn.jcubic.pl/StringIO.py"></script>
<script type="text/python">
f = StringIO()
</script>

(StringIO came from https://svn.python.org/projects/python/trunk/Lib/StringIO.py with removed __main__ because it get executed)

and got error:

Traceback (most recent call last):
NameError: name 'StringIO' is not defined

How can I import python library from URL? Is there a way?

UPDATE found it that in Python 3 and Brython StringIO is in io module but still I would like to know how to import module from url if it's possible.

You need to place it into server side brython/Lib/site-packages directory. It needs to be pure python naturally.

Also note, that you should be able to modify the sys.path search direcotries in with pythonpath setting:

<body onload="brython(
        debug:1, 
        pythonpath:['http://example.com/brython/Lib/', 'http://e... ]
      )" 
>

So note that the paths are actually full URLs to module paths. It took some time to me figure it out as documentation ( currently now at 2018-03-20 brython-3.4.0 ) does not give an example of that.

Also note that touching pythonpath value overwrites the defaults and you need to include them as well. In my understanding those are the current Apache PATH_INFO environment value, that is the current loaded path and the brython source tree Lib/site-packages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM