gopherdata / gophernotes

The Go kernel for Jupyter notebooks and nteract.
MIT License
3.8k stars 264 forks source link

Error importing packages need C: could not import C (no metadata for C) #194

Open leondgarse opened 4 years ago

leondgarse commented 4 years ago

What are you trying to do?

What did you do?

Install OpenBLAS

git clone https://github.com/xianyi/OpenBLAS cd OpenBLAS make make install # This will install OpenBLAS lib to /opt/OpenBLAS/lib export LD_LIBRARY_PATH=/opt/OpenBLAS/lib/:$LD_LIBRARY_PATH CGO_LDFLAGS="-L/opt/OpenBLAS/lib -lopenblas" go install gonum.org/v1/netlib/blas/netlib

Open a `jupyter-notebook` --> `New` --> `Go`,  run:
```go
import (
    "fmt"
    "gonum.org/v1/gonum/mat"
    "gonum.org/v1/gonum/blas/blas64"
    "gonum.org/v1/netlib/blas/netlib"
)

// error loading package "gonum.org/v1/netlib/blas/netlib" metadata: /home/leondgarse/go/pkg/mod/gonum.org/v1/netlib@v0.0.0-20191229114700-bbb4dff026f8/blas/netlib/blas.go:13:8: could not import C (no metadata for C)

Then open a jupyter-notebook --> New --> Go, run again:

import (
    "fmt"
    tf "github.com/tensorflow/tensorflow/tensorflow/go"
)
// error loading package "github.com/tensorflow/tensorflow/tensorflow/go" metadata: /home/leondgarse/go/pkg/mod/github.com/tensorflow/tensorflow@v2.0.0+incompatible/tensorflow/go/attrs.go:21:8: could not import C (no metadata for C)

What did you expect to happen?

Expect importing successfully with no error throws out.

What actually happened?

Importing throws error could not import C (no metadata for C) Selection_147

Another jupyter Go kernel lgo can import tensorflow succefully: Selection_148

What version of Go and Gophernotes are you using?

$ go version
go version go1.13.5 linux/amd64
gophernotes$ git rev-parse HEAD
6124cc7fbaa772aedae66ac41adb2d9de2fbc60b
cosmos72 commented 4 years ago

This is caused by the same issue reported in gomacro https://github.com/cosmos72/gomacro/issues/77 I will investigate it ASAP

leondgarse commented 4 years ago

Understood, thanks for your explain. Glad you are looking into this. :)

cosmos72 commented 4 years ago

Can you execute the shell commands

GO111MODULE=on go get -v github.com/tensorflow/tensorflow/tensorflow/go
GO111MODULE=on go install -v github.com/tensorflow/tensorflow/tensorflow/go

and then try again to import "github.com/tensorflow/tensorflow/tensorflow/go" ?

I am trying to understand if such workaround could work - if it does, I can modify gophernotes to run it automatically.

cosmos72 commented 4 years ago

I tried them myself: no luck.

Thus I just added a workaround: the special command %go111module {on|off} enables/disables GO111MODULE support when importing packages.

By default it is on, but to import packages that expose C types/symbols - such as github.com/tensorflow/tensorflow/tensorflow/go and gonum.org/v1/netlib/blas/netlib, you should turn it off.

Let me know if it helps.

leondgarse commented 4 years ago

Yes, it do works! Selection_149 But import "gonum.org/v1/netlib/blas/netlib" throws another error error executing "/usr/local/go/bin/go build -buildmode=plugin" in directory. It also works well in a script with go run.

// test_netlib.go
package main
import (
    "fmt"
    "gonum.org/v1/gonum/mat"
    "gonum.org/v1/gonum/blas/blas64"
    blas_netlib "gonum.org/v1/netlib/blas/netlib"
)

func main() {
    blas64.Use(blas_netlib.Implementation{})
    zero := mat.NewDense(2, 2, nil)
    fmt.Println(zero)
}

Run

$ CGO_LDFLAGS="-L/opt/OpenBLAS/lib -lopenblas" go run test_netlib.go
&{{2 2 [0 0 0 0] 2} 2 2}

Also tried these two commands

$ GO111MODULE=off CGO_LDFLAGS="-L/opt/OpenBLAS/lib -lopenblas" go run test_netlib.go
&{{2 2 [0 0 0 0] 2} 2 2}

$ GO111MODULE=on CGO_LDFLAGS="-L/opt/OpenBLAS/lib -lopenblas" go run test_netlib.go
go: finding gonum.org/v1/netlib latest
&{{2 2 [0 0 0 0] 2} 2 2}

I think it's another issue though. I'm still trying to solve this.

Thanks for your work. I'm glad tensorflow works. :)