TranscryptOrg / Transcrypt

Python 3.9 to JavaScript compiler - Lean, fast, open!
https://www.transcrypt.org
Apache License 2.0
2.84k stars 214 forks source link

it fails on `import copy` #313

Closed dportabella closed 1 month ago

dportabella commented 7 years ago

this simple file test.py:

import copy

transcrypt fails with:

$ transcrypt -b test.py

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

Error while compiling (offending file last):
    File '/Users/david/test.py', line 1, at import of:
    File '/Users/david/.virtualenvs/py3-data/lib/python3.6/copy.py', line 51, at import of:
    File '/Users/david/.virtualenvs/py3-data/lib/python3.6/types.py', line 4, namely:

    Attempt to import module: sys
    Can't find any of:
        /Users/david/sys.py
        /Users/david/__javascript__/sys.mod.js
        /Users/david/.virtualenvs/py3-data/lib/python3.6/site-packages/transcrypt/modules/sys.py
        /Users/david/.virtualenvs/py3-data/lib/python3.6/site-packages/transcrypt/modules/__javascript__/sys.mod.js
        /Users/david/.virtualenvs/py3-data/bin/sys.py
        /Users/david/.virtualenvs/py3-data/bin/__javascript__/sys.mod.js
        /Users/david/.virtualenvs/py3-data/lib/python36.zip/sys.py
        /Users/david/.virtualenvs/py3-data/lib/python36.zip/__javascript__/sys.mod.js
        /Users/david/.virtualenvs/py3-data/lib/python3.6/sys.py
        /Users/david/.virtualenvs/py3-data/lib/python3.6/__javascript__/sys.mod.js
        /Users/david/.virtualenvs/py3-data/lib/python3.6/lib-dynload/sys.py
        /Users/david/.virtualenvs/py3-data/lib/python3.6/lib-dynload/__javascript__/sys.mod.js
        /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sys.py
        /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/__javascript__/sys.mod.js
        /Users/david/.virtualenvs/py3-data/lib/python3.6/site-packages/sys.py
        /Users/david/.virtualenvs/py3-data/lib/python3.6/site-packages/__javascript__/sys.mod.js

Another project that also has import copy, it fails with:

$ transcrypt -b test.py

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

Error while compiling (offending file last):
    File '/Users/david/test.py', line 5, at import of:
    File '/Users/david/.virtualenvs/py3-data/lib/python3.6/copy.py', line 51, at import of:
    File '/Users/david/.virtualenvs/py3-data/lib/python3.6/types.py', line 15, at import of:
    File '/Users/david/.virtualenvs/py3-data/lib/python3.6/site-packages/lxml/__init__.py', line 9, at import of:
    File '/Users/david/.virtualenvs/py3-data/lib/python3.6/os.py', line 25, at import of:
    File '/Users/david/.virtualenvs/py3-data/lib/python3.6/abc.py', line 6, at import of:
    File '/Users/david/.virtualenvs/py3-data/lib/python3.6/_weakrefset.py', line 5, namely:

    Attempt to import module: _weakref
    Can't find any of:
        /Users/david/_weakref.py
        /Users/david/__javascript__/_weakref.mod.js
        /Users/david/.virtualenvs/py3-data/lib/python3.6/site-packages/transcrypt/modules/_weakref.py
        /Users/david/.virtualenvs/py3-data/lib/python3.6/site-packages/transcrypt/modules/__javascript__/_weakref.mod.js
        /Users/david/.virtualenvs/py3-data/bin/_weakref.py
        /Users/david/.virtualenvs/py3-data/bin/__javascript__/_weakref.mod.js
        /Users/david/.virtualenvs/py3-data/lib/python36.zip/_weakref.py
        /Users/david/.virtualenvs/py3-data/lib/python36.zip/__javascript__/_weakref.mod.js
        /Users/david/.virtualenvs/py3-data/lib/python3.6/_weakref.py
        /Users/david/.virtualenvs/py3-data/lib/python3.6/__javascript__/_weakref.mod.js
        /Users/david/.virtualenvs/py3-data/lib/python3.6/lib-dynload/_weakref.py
        /Users/david/.virtualenvs/py3-data/lib/python3.6/lib-dynload/__javascript__/_weakref.mod.js
        /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_weakref.py
        /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/__javascript__/_weakref.mod.js
        /Users/david/.virtualenvs/py3-data/lib/python3.6/site-packages/_weakref.py
        /Users/david/.virtualenvs/py3-data/lib/python3.6/site-packages/__javascript__/_weakref.mod.js
dportabella commented 7 years ago

I tried also on docker, with the same result:

docker run -ti python bash
echo "import copy" >test.py
pip install transcrypt
transcrypt -b test.py

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

Error while compiling (offending file last):
    File '//test.py', line 1, at import of:
    File '/usr/local/lib/python3.6/copy.py', line 51, at import of:
    File '/usr/local/lib/python3.6/types.py', line 4, namely:

    Attempt to import module: sys
    Can't find any of:
        //sys/__init__.py
        //sys/__javascript__/__init__.mod.js
        /usr/local/lib/python3.6/site-packages/transcrypt/modules/sys.py
        /usr/local/lib/python3.6/site-packages/transcrypt/modules/__javascript__/sys.mod.js
        /usr/local/bin/sys.py
        /usr/local/bin/__javascript__/sys.mod.js
        /usr/local/lib/python36.zip/sys.py
        /usr/local/lib/python36.zip/__javascript__/sys.mod.js
        /usr/local/lib/python3.6/sys.py
        /usr/local/lib/python3.6/__javascript__/sys.mod.js
        /usr/local/lib/python3.6/lib-dynload/sys.py
        /usr/local/lib/python3.6/lib-dynload/__javascript__/sys.mod.js
        /usr/local/lib/python3.6/site-packages/sys.py
        /usr/local/lib/python3.6/site-packages/__javascript__/sys.mod.js

Aborted
JdeH commented 7 years ago

Hi,

Unfortunately the 'copy' standard library is not yet supported by Transcrypt.

TS mostly relies on JS libs:

http://sterlicht.alwaysdata.net/transcrypt.org/docs/html/what_why.html#the-ecosystem-different-batteries

We strive to make a limited number of standard libs available, to ease the transition from CPython.

Currently the following standard modules are available for Transcrypt: math, cmath, re, itertools, time, random (most important functions only), logging, warnings, turtle. There's also a port of a small part of Numpy called Numscrypt, available separately from PyPi.

Kind regards Jacques

dportabella commented 7 years ago

Ok, thanks. do you plan to support the "collections" library?

JdeH commented 7 years ago

There were no plans in that direction, but actually that may be a useful one to support. In general for libraries we rely on third party contributions.

KR Jacques

harto commented 6 years ago

@JdeH I'm interested in a port of the collections module too. Where's a good place to discuss the design of it?

JdeH commented 6 years ago

@harto

I've opened #442 as a place to discuss this.

Kind regards Jacques

JennaSys commented 4 years ago

Revisiting this. I know copy is not supported yet, but the message I get is slightly different so I wanted to add to this thread:

I'm trying to use deepcopy from the built-in copy module copy.deepcopy(), but Transcrypt complains that it can't find PyStringMap when trying to import it import copy

Error while compiling (offending file last):
        File '/home/data/Python/Transcrypt/demo3/app.py', line 11, at import of:
        File 'copy', line 60, namely:

        Import error, can't find any of:
                /home/data/Python/Transcrypt/demo3/org/python/core/PyStringMap.py
                /home/data/Python/Transcrypt/demo3/org/python/core/PyStringMap.js
                /home/data/Python/Transcrypt/demo3/venv/lib/python3.7/site-packages/transcrypt/modules/org/python/core/PyStringMap.py
                /home/data/Python/Transcrypt/demo3/venv/lib/python3.7/site-packages/transcrypt/modules/org/python/core/PyStringMap.js
                /home/data/Python/Transcrypt/demo3/venv/bin/org/python/core/PyStringMap.py
                /home/data/Python/Transcrypt/demo3/venv/bin/org/python/core/PyStringMap.js
                /usr/lib/python3.7/org/python/core/PyStringMap.py
                /usr/lib/python3.7/org/python/core/PyStringMap.js
                /usr/lib/python3.7/lib-dynload/org/python/core/PyStringMap.py
                /usr/lib/python3.7/lib-dynload/org/python/core/PyStringMap.js
                /home/data/Python/Transcrypt/demo3/venv/lib/python3.7/site-packages/org/python/core/PyStringMap.py
                /home/data/Python/Transcrypt/demo3/venv/lib/python3.7/site-packages/org/python/core/PyStringMap.js

...

In the mean time I've had some luck with using the lodash.clonedeep() JS library, but it seems to return dictionaries that have to be run through Python dict() to be valid.

JennaSys commented 3 years ago

After getting a bit more experience with Transcrypt and re-reading this, I realized my previous post was already answered in that the Python copy library itself is not implemented.

Related to this though, I see there are copy and deepcopy functions that end up in the Transcrypt JS runtime module. However that deepcopy function does not appear to actually deep copy JavaScript objects. I have worked around this issue by using a JS library (both deepcopy and lodash.cloneDeep work), but was wondering what the relationship of the deepcopy in the Transcrypt runtime is to the Python copy.deepcopy function, and if so are there known limitations of that one? Or am I just misunderstanding something?

JennaSys commented 1 month ago

copy module with copy and deepcopy is currently added in dev_3.9.3 branch to be released in v3.9.3 Need to do a bit more testing on it before releasing.