grumpyhome / grumpy

Grumpy is a Python to Go source code transcompiler and runtime.
Apache License 2.0
420 stars 18 forks source link

how to transpile python project with local modules? #149

Closed xukku closed 4 years ago

xukku commented 4 years ago

in example

i have some

main file script.py


import X.File
...
X.File.save(host + "_" + username + "/" + src_path, res)
...

and my local module ./X/File.py


def save(name, content, append = False):
    """Write or append content to file"""
        ...
alanjds commented 4 years ago

It occurs automatically.

I suggest you to create a sample module tree and run on CPython (python script.py). It will create a folder __pycache__ containing one file ended with PYC for every imported PY file: ./X/File.py will generate a ./X/__pycache__/File.cpython-27.pyc, for example. This is the standard CPython behavior after Python 3.2. The Python 2.7 gets this behavior when you install the importlib2 module.

Alright. Now you run the same main script with Grumpy (grumpy script.py). It will create a folder __pycache__ containing one FOLDER ended with PYC for every imported PY file: ./X/File.py will generate a ./X/__pycache__/File.grumpy-XYZ.pyc, for example.

Inside this folder will be the whole Golang source tree needed. You can go on every folder generated along the original .PY files and collect the transpiled versions.

If you want to see more internals, including every single folder generated, you can run Grumpy in debug mode: grumpy -v debug script.py

Some of this information is explained better on this link: https://labs.getninjas.com.br/released-grumpy-runtime-v0-3-0-a05f1cf8e111