I am trying to import the os module in Brython, but no matter what I do, no matter what I try, I am unable to. I get the following error (in the Firefox console):
"TypeError: obj is undefined for module os" brython.js:6329:21
"message: undefined" brython.js:6330:1
"filename: http://localhost:8000/src/brython.js" brython.js:6331:1
"linenum: 4418" brython.js:6332:1
"Javascript error" TypeError: obj is undefined
Stack-Trace:
$B.get_class@http://localhost:8000/src/brython.js:4418:5
$test@http://localhost:8000/src/brython.js:8873:1
$SetDict.__le__@http://localhost:8000/src/brython.js:8830:50
getattr/method@http://localhost:8000/src/brython.js:5039:8
$module<@http://localhost:8000/src/brython.js line 6329 > eval:966:41
@http://localhost:8000/src/brython.js line 6329 > eval:1:14
run_py@http://localhost:8000/src/brython.js:6329:1
import_py@http://localhost:8000/src/brython.js:6310:8
import_from_stdlib_static@http://localhost:8000/src/brython.js:6378:22
$B.$import@http://localhost:8000/src/brython.js:6454:57
@http://localhost:8000/src/brython.js line 3931 > eval:11:1
brython@http://localhost:8000/src/brython.js:3931:7
onload@http://localhost:8000/boolean/boolean.html:1:1
brython.js:3940:43
"Traceback (most recent call last):
RuntimeError: TypeError: obj is undefined"
When I do this:
import os
def foo(ev):
print(os.getcwd())
doc["submit"].bind('click', foo)
This is just an example - originally the code was much longer. I am using the latest Brython version (3.2.0). The import of os and the functions work fine when I try them on the Brython page in the console they provide, and so far os is the only import that I cannot get to work. Any help is appreciated, thank you for your time
Update: It works now, but I have no idea why and I don't understand - if someone could explain what I did wrong that would be great, in case I get a similar problem in the future I looked around for alternatives to os (I want to read .txt files from a directory - I know how to do this in "vanilla" Python using os) and found a solution using glob, which I tried to import. Brython doesn't seem to come with glob, so I put the glob.py in the lib folder - but I also saw that glob imports os, so I wondered if it works, which it did. Then I tried to import os again, and now it works and I don't know why.
from browser import document as doc, alert, html
from glob import glob
import os
# ... #
def foo(ev):
print(os.getcwd())
doc["submit"].bind("click", foo)
Now works. I don't get it, why does it work if I import glob and then os?