Relative import
skelouse-secondary opened this issue · 5 comments
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.
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
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
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.
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.
Updated. Latest gophernotes (v0.7.5) can now import local packages.