Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Messes up python's import? #81

Open
wusspuss opened this issue Jan 27, 2022 · 1 comment
Open

Messes up python's import? #81

wusspuss opened this issue Jan 27, 2022 · 1 comment

Comments

@wusspuss
Copy link

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

@NasaGeek
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants