gopherdata / gophernotes

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

Relative import #237

Closed skelouse-secondary closed 2 years ago

skelouse-secondary commented 2 years ago

I'm trying to set up an example usage document of my go module. The directory is structured as

go.mod
x.go
y.go
z.go
example/
    x.ipynb

How would I import relative package in x.ipynb without doing a go get?

If I do something such as:

go.mod
x.go
...
example/
    main.go

I'm able to import the relative module with no issue. With Jupyter it tries to do a go get, and the can't find the unpublished package.

Basically the same text in example/main.go doesn't work inside x.ipynb Is this expected? In python I can spin up a jupyter notebook in my module, and do relative imports all day long. May just be that I'm new to go.

cosmos72 commented 2 years ago

Go toolchain does not directly support importing of unpublished packages - for that, you need to manually edit the go.mod file of the code that wants to import the unpublished packages. At the moment, gophernotes autogenerates such go.mod, and there is no hook to edit/customize it.

There is a proposal somewhere (don't remember the exact link) to add a new syntax for relative imports, but nobody is currently working on it

t-anyu commented 2 years ago

Hey, can you specify where gophernotes autogenerates this go.mod? I can't get unpublished(local repo) work with gophernotes. Tried already with go111module on and off and creating symlinks to src/ path and gomacro.imports, but nothing works. The only ones working for me are public packages.

Thanks

cosmos72 commented 2 years ago

It is created in $GOPATH/src/gomacro.imports/PACKAGE/FULL/PATH

For example, if executing go env GOPATH prints /Users/myname/go, then

import "gonum.org/v1/gonum/floats"

creates the file /Users/myname/go/src/gomacro.imports/gonum.org/v1/gonum/floats/go.mod

Beware that you cannot just create it beforehand, because it gets overwritten every time: if you make it read-only (or immutable - for example on Linux you can use chattr +i FILENAME as root) then the import will fail with something like:

error removing file "/Users/myname/go/src/gomacro.imports/gonum.org/v1/gonum/floats/go.mod": remove /Users/myname/go/src/gomacro.imports/gonum.org/v1/gonum/floats/go.mod: operation not permitted

You'd need to somehow suspend gophernotes immediately after it created the go.mod file, edit it, then resume gophernotes - which very likely requires modifying gophernotes sources (actually, of underlying interpreter gomacro), because the time window between creation and use of such go.mod files is very short.

cosmos72 commented 2 years ago

I have just added the ability to import local packages to the underlying interpreter - see gomacro commit 8715bf81596c6890829faa99f85e9b4085bab8f6

I will update gophernotes to use it as soon as I can.

cosmos72 commented 2 years ago

Updated. Latest gophernotes (v0.7.5) can now import local packages.