Transcrypt icon indicating copy to clipboard operation
Transcrypt copied to clipboard

problem importing stubs

Open davidedelvento opened this issue 6 years ago • 2 comments

I would like to import the stubs use py.test (with which I am running the tests). Unfortunately that is not working for me.

(transcrypt_venv) $ python -c "import transcrypt"

(transcrypt_venv) $ python -c "import org.transcrypt.stubs.browser"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'org'

(transcrypt_venv) $ python -c "import transcrypt.stubs.browser"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'transcrypt.stubs'

(transcrypt_venv) $ python -c "import transcrypt; print(dir(transcrypt))"
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
(transcrypt_venv) $ transcrypt 

Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.7.16
Copyright (C) Geatec Engineering. License: Apache 2.0

I installed transcrypt with pip in a virtualenv (as it might be clear from the prompt). What am I missing?

davidedelvento avatar Mar 05 '20 12:03 davidedelvento

I found a horrible workaround, but still better than being stuck and not being able to use the stubs.

First, there is a missing file for CPython to be able to import the module, so you must run something like

touch ~/transcrypt_venv/lib/python3.7/site-packages/transcrypt/modules/org/transcrypt/__init__.py

(obviously your path to transcript will be different from my ~/transcrypt_venv/)

After that, you must add the right module search path, which I would to with PYTHONPATH to avoid polluting the generated javascript with irrelevant material. Sadly, we cannot use PYTHONPATH, because that adds at the beginning of the search path and breaks too many things, particularly re. Hence I do the following in the files which I want to test in python and use __pragma__

import sys  # TODO very ugly see https://github.com/QQuick/Transcrypt/issues/711 if a better option becomes available
if "pytest" in sys.modules:
    prefix = sys.path[-1]
    sys.path.append(prefix + "/transcrypt/modules/")
    from org.transcrypt.stubs.browser import __pragma__

I know the import is executed anyway (since I'm either in pytest, and hence is executed, or in trascrypt and then it is transpiled to javascript regardless), but this way makes it clear to the reader, i.e. my future self

davidedelvento avatar Apr 18 '20 02:04 davidedelvento

I had the same issue when running python 3.7.9 (intentionally not the latest) without realizing that I had to get the corresponding version of Transcrypt, per https://github.com/QQuick/Transcrypt/issues/792

scottlawton avatar Jan 17 '22 22:01 scottlawton