Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

No module named xxx problem ,when the xxx is a so file .. #42

Open
JH6588 opened this issue May 29, 2021 · 2 comments
Open

No module named xxx problem ,when the xxx is a so file .. #42

JH6588 opened this issue May 29, 2021 · 2 comments

Comments

@JH6588
Copy link

JH6588 commented May 29, 2021

Describe what happened:
the first line of py code
from xxx import play

Describe what you expected:
the xxx is a so file.
Run the py script in command line is fine

Steps to reproduce the issue:
I read the the py script as string in go named code ,then python3.PyRun_SimpleString(code)
Not work , ModuleNotFoundError: no module named 'xxx'

@christian-korneck
Copy link
Contributor

christian-korneck commented May 29, 2021

the path to the dir that contains the python module that you want to import needs to be in Python's sys.path list.

You can print the list with something like:

pycode := `
import sys
for path in sys.path:
  print(path)
`
python3.PyRun_SimpleString(pycode)

and also append a missing path, if needed

dir := "/some/custom/site-packages"
python3.PyRun_SimpleString("import sys\nsys.path.append(\"" + dir + "\")")

Here's a more complete example that adds the same path as the compiled Go program:

dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
	log.Fatal(err)
}
ret := python3.PyRun_SimpleString("import sys\nsys.path.append(\"" + dir + "\")")
if ret != 0 {
	log.Fatalf("error appending '%s' to python sys.path", dir)
}

for completeness: instead of doing this in a PyRun_SimpleString() (which only has limited error handling) you could also use PySys_GetObject("path") + PySys_SetPath().

@JH6588
Copy link
Author

JH6588 commented May 29, 2021

It's very helpful ,my problem was solved . Thank you.

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

No branches or pull requests

2 participants