difference between from <package_name> import <module_name> vs import <package_name>.<module_name> in the __init__ file of a package - python-3.9

Is there a difference between these 3 methods to import a module in the __init__.py file of a package?
Assuming that the module.py and the __init__.py are in the same directory and I am importing the package in my program as import package
import package.module
from package import module
from . import module
I did all of them and apparently, all worked with the same results.

Related

Specify base package in protobuf compilation in golang

I've got golang package github.com/user/protoapp, in this package I have folder proto containing protobuf files.
github.com/user/protoapp
|-proto
|-proto/app1
|-proto/app2
proto files in app1 and app2 have corresponing packages app1 and app;
Proto file from proto/app1 is importing file from proto/app2 like import "app2/messages.proto"; after compillation in app1.pb.go it becomes import app2 and protoapp fails to compile. How do I make imports in *.pb.go files become import "github.com/user/protoapp/proto/app2" rather than import "app2"?
you need to make your import in your proto a fully qualified path like in Go:
instead of import "app2/messages.proto";
try import "github.com/user/protoapp/proto/app2/messages.proto";
Never specify the half path "/app2/messages.proto" it won't be work.
Specify the full import name
import "github.com/user/protoapp/proto/app2/messages.proto";
and
Do define with the package name
//if we does'nt add package we define it will show error
package messagedata;

i am not able to import a package from another directory to main.go

while i tried adding import config "./config" in main.go and try to save it and run then it remove the import config "./" section
my code structure is
-config/config.go
model/model.go
main.go
while running i got
[go] can't load package: package .: found packages config (config.go) and main (main.go) in /Users/Desktop/inventory-backend
In go we import modules by specifying the path as e.g.
import "gopath/projectName/config"

Import package & type

Here is my problem and my project structure
src
|-->config
|--> config.go
|-->otherPackage
|--> otherFile.go
|-->main.go
I have a type on config.go that I would like to use in otherFile.go
But when I tried to add it to the import here theses issues:
imported and not used.
undefined: Config
Although I use it in the function declaration
function(target float64, entries [2]float64, config Config)
What is the problem with this?
I tried to import it with
import (
"fmt"
"math"
"../config"
)
You cannot "import from a package". All you can do is "import the whole package". That means if you import "full/import/path/of/foo" and that package declared itself to be called foo via package foo at the beginning, then everything in this package has to be qualified by foo:
foo.Config
If you package is called config than declaring a variable config will shaddow the whole package: so you have to:
Rename the config variable to e.g. cfg
Reference Config from package config with its qualified name config.Config

How to include external file in Go?

I'm using LiteIDE for Go. I have a Go file located here:
/Users/username/go/src/src/Helper/Helper.go
When I include the file using:
import "../Helper"
I get this error:
can't load package: /Users/username/go/src/src/projectA/main.go:4:8:
local import "../Helper" in non-local package
Any ideas what I'm doing wrong?
You import packages by import path. For package Helper, located in $GOPATH/src/Helper/, use:
import "Helper"
While they can work in some cases, relative paths aren't supported by the go toolchain, and are discouraged.

Unable to build freegeoip in Golang

Not familiar with Go but I'm trying to set up freegeoip and I keep getting this error when I try to build it:
go get github.com/fiorix/freegeoip
cd ~/go/src/github.com/fiorix/freegeoip
go build
../../oschwald/maxminddb-golang/decoder.go:4:2: import "bytes": cannot find package
db.go:8:2: import "compress/gzip": cannot find package
../../oschwald/maxminddb-golang/decoder.go:5:2: import "encoding/binary": cannot find package
encoder.go:8:2: import "encoding/csv": cannot find package
../../robertkrimen/otto/script.go:5:2: import "encoding/gob": cannot find package
../../robertkrimen/otto/builtin.go:4:2: import "encoding/hex": cannot find package
encoder.go:9:2: import "encoding/json": cannot find package
encoder.go:10:2: import "encoding/xml": cannot find package
db.go:9:2: import "errors": cannot find package
db.go:10:2: import "fmt": cannot find package
../../robertkrimen/otto/dbg/dbg.go:62:2: import "io": cannot find package
../../robertkrimen/otto/parser/parser.go:40:2: import "io/ioutil": cannot find package
../../robertkrimen/otto/dbg/dbg.go:63:2: import "log": cannot find package
../../robertkrimen/otto/builtin.go:5:2: import "math": cannot find package
../../oschwald/maxminddb-golang/decoder.go:7:2: import "math/big": cannot find package
../../robertkrimen/otto/builtin_math.go:5:2: import "math/rand": cannot find package
../../oschwald/maxminddb-golang/reader.go:7:2: import "net": cannot find package
db.go:14:2: import "net/http": cannot find package
../../robertkrimen/otto/builtin.go:6:2: import "net/url": cannot find package
../../howeyc/fsnotify/fsnotify_linux.go:12:2: import "os": cannot find package
db.go:16:2: import "path/filepath": cannot find package
../../oschwald/maxminddb-golang/decoder.go:8:2: import "reflect": cannot find package
../../robertkrimen/otto/dbg/dbg.go:65:2: import "regexp": cannot find package
package github.com/fiorix/freegeoip
imports runtime: import "runtime": cannot find package
../../robertkrimen/otto/parser/error.go:5:2: import "sort": cannot find package
../../robertkrimen/otto/token/token.go:5:2: import "strconv": cannot find package
../../howeyc/fsnotify/fsnotify_linux.go:13:2: import "strings": cannot find package
../../howeyc/fsnotify/fsnotify_linux.go:14:2: import "sync": cannot find package
../../howeyc/fsnotify/fsnotify_linux.go:15:2: import "syscall": cannot find package
../../robertkrimen/otto/builtin_date.go:5:2: import "time": cannot find package
../../robertkrimen/otto/dbg/dbg.go:68:2: import "unicode": cannot find package
../../robertkrimen/otto/builtin.go:10:2: import "unicode/utf16": cannot find package
../../robertkrimen/otto/parser/lexer.go:11:2: import "unicode/utf8": cannot find package
../../howeyc/fsnotify/fsnotify_linux.go:16:2: import "unsafe": cannot find package
EDIT:
My GOPATH & GOROOT are:
GOROOT=/usr/local/go
GOPATH=$HOME/go

Resources