Transcrypt
Transcrypt copied to clipboard
Transcrypt fails to find relative imports
Of a low priority to me, but thought I'd report this anyways. Relative imports (from .mod import ...
) don't seem to work in Transcrypt, and produce a 'could not find' error.
Test case:
main.py
:
import parentmod
parentmod.afunc()
parentmod/__init__.py
:
from .childmod import afunc
parentmod/childmod.py
:
def afunc():
print('childmod: afunc!')
Running python main.py
works as expected, but trying to transcrypt main.py
results in the following error:
Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.6.4
Copyright (C) Geatec Engineering. License: Apache 2.0
Error in program main.py, module childmod, line 1:
Attempt to load module: childmod
Can't find any of:
/home/daboross/Projects/Python/TranscryptTest/src/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/src/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib/python3.5/site-packages/transcrypt/modules/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib/python3.5/site-packages/transcrypt/modules/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/bin/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/bin/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib64/python35.zip/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib64/python35.zip/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib64/python3.5/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib64/python3.5/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib64/python3.5/plat-linux/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib64/python3.5/plat-linux/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib64/python3.5/lib-dynload/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib64/python3.5/lib-dynload/__javascript__/childmod.mod.js
/usr/lib64/python3.5/__javascript__/childmod.mod.js
/usr/lib64/python3.5/__javascript__/childmod.mod.js
/usr/lib/python3.5/__javascript__/childmod.mod.js
/usr/lib/python3.5/__javascript__/childmod.mod.js
/usr/lib/python3.5/plat-linux/__javascript__/childmod.mod.js
/usr/lib/python3.5/plat-linux/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib/python3.5/site-packages/__javascript__/childmod.mod.js
/home/daboross/Projects/Python/TranscryptTest/env/lib/python3.5/site-packages/__javascript__/childmod.mod.js
/usr/lib/python3.5/site-packages/__javascript__/childmod.mod.js
/usr/lib/python3.5/site-packages/__javascript__/childmod.mod.js
Aborted
Another test case with slightly different error message:
When parentmod/__init__.py
is written as:
from . import childmod
afunc = childmod.afunc
Transcrypt fails with:
Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.6.4
Copyright (C) Geatec Engineering. License: Apache 2.0
Error in program main.py, module parentmod, line 1: Can't import module 'parentmod'
Aborted
This also works fine with python main.py
.
Relative imports have not been added. The import mechanism was deliberately simplified. Currently I'd say: lets leave it out.
@daboross You indicate low prio for you. Anyone else really wants to have this? I'd rather not, but always open to good arguments...
KR J
@JdeH I have an avid interest in bringing special functions to the browser by transpiling mpmath and Transcrypt looks like the best tool available. Unfortunately the library is full of relative imports just like the test cases above. Before I start modifying just about every file in the library, is there any chance that relative imports will be supported in the near future? Thanks!
After comment of @paulmasson:
Will look at this a bit closer and get back on it a.s.a.p.
same thing here, need statistical analysis libs :)
I heavily rely on python modularization in order to keep the code clean and well structured. Having code "physically" separated into modules and sub-modules is - for me - a big help in de-coupling complexity.
Certainly modules are needed. But they function correctly. It's only the relative imports that are currently lacking.
Hello - just curious, what is the status on this issue ? Thanks.
Hasn't been picked up by anyone yet. Since it has been open rather long I label it a restriction for now and close it. However feel free to reopen if anyone wants to work on it. Be warned that it is complicated due to the mix of CPython, Transcrypt and project local imports.
Bokeh also uses these imports. And matplotlib.
Be warned that it is complicated due to the mix of CPython, Transcrypt and project local imports.
Could you elaborate?
And what's the easiest workaround? One of https://stackoverflow.com/a/16985066/594456?
I just ran into this as well expecting relative imports to work. The limitation should probably be added to the docs.
My use case was just for convenience, as some of my imports were getting more verbose than I would like and I wanted to shorten them. I worked around it by using absolute imports in the package __init__.py file and imported objects from there when I needed to use them.