Transcrypt icon indicating copy to clipboard operation
Transcrypt copied to clipboard

Transcrypt fails to find relative imports

Open daboross opened this issue 8 years ago • 12 comments

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

daboross avatar Jan 14 '17 08:01 daboross

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.

daboross avatar Jan 14 '17 08:01 daboross

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 avatar Jan 16 '17 11:01 JdeH

@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!

paulmasson avatar Apr 10 '17 22:04 paulmasson

After comment of @paulmasson:

Will look at this a bit closer and get back on it a.s.a.p.

JdeH avatar Apr 12 '17 13:04 JdeH

same thing here, need statistical analysis libs :)

artem-v-shamsutdinov avatar Apr 30 '17 18:04 artem-v-shamsutdinov

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.

mythsmith avatar Jun 19 '17 12:06 mythsmith

Certainly modules are needed. But they function correctly. It's only the relative imports that are currently lacking.

JdeH avatar Dec 17 '17 14:12 JdeH

Hello - just curious, what is the status on this issue ? Thanks.

andyexpress avatar Jan 24 '18 10:01 andyexpress

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.

JdeH avatar Jan 29 '18 16:01 JdeH

Bokeh also uses these imports. And matplotlib.

Trass3r avatar Jan 30 '19 12:01 Trass3r

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?

Trass3r avatar Jun 01 '19 09:06 Trass3r

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.

JennaSys avatar Feb 12 '22 22:02 JennaSys