Open JH6588 opened 3 years ago
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()
.
It's very helpful ,my problem was solved . Thank you.
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
,thenpython3.PyRun_SimpleString(code)
Not work , ModuleNotFoundError: no module named 'xxx'