sbinet / go-python

naive go bindings to the CPython2 C-API
Other
1.52k stars 138 forks source link

How to import 3rd packages in the main python file #94

Open camelhehe opened 4 years ago

camelhehe commented 4 years ago

I just found there is no any 3rd packages were imported in your test examples. I tried to import 3rd package, import numpy in a py file, then go run main.go, will report a failed import. Please tell me how to import other 3rd packages installed in anaconda2/lib/python2.7/site-packages/

thanks

camelhehe commented 4 years ago

test.py import numpy import sklearn

def a(): return

main.go t := python.PyImport_ImportModule("test")

t actually is nil

Just remove import numpy and sklearn from test.py, then it works.

sbinet commented 4 years ago

you're probably mixing python libraries (i.e.: trying to import numpy from anaconda while you compiled go-python against another python2 interpreter.)

try this program to display the error:

package main

import (
    "log"

    "github.com/sbinet/go-python"
)

func init() {
    err := python.Initialize()
    if err != nil {
        panic(err.Error())
    }
}

func main() {
    m := python.PyImport_ImportModule("numpy")
    if m == nil {
        python.PyErr_Print()
        log.Fatalf("could not import numpy")
    }
    m.DecRef()
}