tohil icon indicating copy to clipboard operation
tohil copied to clipboard

Messes up python's import?

Open wusspuss opened this issue 3 years ago • 1 comments

tohil::exec {import mymodule} or tohil::import mymodule will both complain No module named 'mymodule' (while evaluating python code) from python code executed by tohil whereas a mymodule.py exists in same dir the script is running from. import mymodule from normal python works fine. Importing system modules through tohil works fine. Tohil passes all tests.

I also noticed python's cffi module is broken in a similar manner: cffi itself imports fine, but smth like ffi.set_source("_test", "..."); ffi.compile(); from _test import lib Will again complain of module _test not being found, whereas it will work fine in standalone python

wusspuss avatar Jan 27 '22 14:01 wusspuss

tohil initializes the Python interpreter differently than the python executable. This results in the contents of sys.path being subtly different.

A demonstration:

I [nix-shell:~/scratch]$ tclsh <<< "package require tohil; tohil::import sys; tohil::exec {print(sys.path)}"
['/nix/store/1cc2qcz3p1sisss7a0w0b82w8zki6cv1-python3.8-tohil-4.4.2/lib/python3.8/site-packages', '/nix/store/ksv01pa19hgnpyni4qvc90bq5y0y6n7g-python3-3.8.12/lib/python3.8/site-packages', '/nix/store/ksv01pa19hgnpyni4qvc90bq5y0y6n7g-python3-3.8.12/lib/python38.zip', '/nix/store/ksv01pa19hgnpyni4qvc90bq5y0y6n7g-python3-3.8.12/lib/python3.8', '/nix/store/ksv01pa19hgnpyni4qvc90bq5y0y6n7g-python3-3.8.12/lib/python3.8/lib-dynload']

I [nix-shell:~/scratch]$ python -c "import sys; print(sys.path)"
['', '/nix/store/1cc2qcz3p1sisss7a0w0b82w8zki6cv1-python3.8-tohil-4.4.2/lib/python3.8/site-packages', '/nix/store/ksv01pa19hgnpyni4qvc90bq5y0y6n7g-python3-3.8.12/lib/python3.8/site-packages', '/nix/store/ksv01pa19hgnpyni4qvc90bq5y0y6n7g-python3-3.8.12/lib/python38.zip', '/nix/store/ksv01pa19hgnpyni4qvc90bq5y0y6n7g-python3-3.8.12/lib/python3.8', '/nix/store/ksv01pa19hgnpyni4qvc90bq5y0y6n7g-python3-3.8.12/lib/python3.8/lib-dynload']

Note the presence of '' in the Python example, which tells the Python import machinery to look in the present directory for modules. This will instead be a path to a Python script's parent directory if you run one.

It might be worth emulating this behavior in tohil, but for the time being you can resolve your issue by manually prepending sys.path with '' before attempting the import.

NasaGeek avatar Jan 27 '22 22:01 NasaGeek