go-python / gopy

gopy generates a CPython extension module from a go package.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

import issues with modules [likely the same issue as #246]

e-gun opened this issue · comments

commented

Hello,

I built a module with pygo. I launched python in the out directory. I import-ed the module. All of the functions behaved as expected. Happy camper. Thanks for all of your hard work.

But the module presently can't be used with my project since from mygopyoutputdir import mygopymodule fails.

This looks like a very generic issue. If we try the same with _examples/hi from go-python/gopy the same problem emerges.

[1] works if you are in the right directory

testing/hi/ % cd out
hi/out/ % ls
Makefile  __init__.py  __pycache__/  _hi.so*  build.py  go.py  hi.c  hi.go  hi.py  hi_go.h  hi_go.so  patch-leaks.go
hi/out/ % python3
Python 3.9.4 (default, Apr  5 2021, 01:50:46)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hi
>>> hi.Concat('a', 'b')
'ab'
>>> quit()

[2] fails if you try to import from above

testing/hi/ % python3
Python 3.9.4 (default, Apr  5 2021, 01:50:46)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from out import hi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/erik/go/src/nowhere.com/testing/hi/out/hi.py", line 23, in <module>
    import go
ModuleNotFoundError: No module named 'go'
>>> from out.hi import hi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/erik/go/src/nowhere.com/testing/hi/out/hi.py", line 23, in <module>
    import go
ModuleNotFoundError: No module named 'go'
>>>

The second attempt was motivated by the comments in out/hi.py itself:

# to use this code in your end-user python file, import it as follows:
# from hi import hi
# and then refer to everything using hi. prefix
# packages imported by this package listed below:

import go

Thanks for your attention.

commented

how to fix this by hand...

in order to achieve from out import hi you need to edit the import statements for go and/or _hi in go.py and hi.py

import _hi should be from hi import _hi, etc.

more generically: from myoutputdir import _mygolangmodulename

you need different edits if you call this from still higher up: go.py and mygolangmodulename.py will need to read from parent.child import _mygolangmodulename, etc.

sed -i "s/import _hi/from hi import _hi/" out/go.py
sed -i "s/import _hi/from hi import _hi/" out/hi.py
sed -i "s/import go/from hi import go/" out/hi.py

@justinfx fixes I believe have fixed this issue. reopen if not.