Poetry: create a package from a part of a project - python-poetry

I am using Poetry to build some package. For simplicity sake the structure of the repository looks something like this:
my_package
pyproject.toml
service
client
client.py
data.py
__init__.py
__init__.py
But I want the package is named my_package_client and includes just client directory. So that after installation I could import like this:
from my_package_client import Client
I tried to write this in pyproject.toml:
name = "my_project_client"
packages = [
{include = "client", from = "service"}
]
But obviously after installation I only can import this way:
from service.client import Client

As far as I know, there is no way to "alias" the package name in Poetry. I worked around this by adding an extra package between service and client, like this:
my_package
pyproject.toml
service
my_project_client
client
client.py
data.py
__init__.py
__init__.py
pyproject.toml (note dashes vs. underscores):
name = "my-project-client"
packages = [
{include = "my_project_client", from = "service"}
]
Add this to the __init__.py of my_project_client to get the namespace you want:
from .client import Client
After publishing to PyPI, install the package in a different repo:
pip install my-project-client
Then you can import like this:
from my_project_client import Client

Related

Cannot import package "google.golang.org/api/compute/v1"

I am using cloud function to auto create some VM instance. Before, I was success in my use-case. You can view it here.Now, I want to update some line in metadata script. But when I save the code, it tell me some package I was import cannot find. Logs is
Build failed: src/cloudfunctions/function.go:9:2: cannot find package "google.golang.org/api/compute/v1" in any of:
/usr/local/go/src/google.golang.org/api/compute/v1 (from $GOROOT)
/workspace/src/google.golang.org/api/compute/v1 (from $GOPATH); Error ID: 2f5e35a0
But I was view document in https://pkg.go.dev/google.golang.org/api/compute/v1, usage example import like this import "google.golang.org/api/compute/v1", as same as me.
So, who can tell me what is $GOROOT and $GOPATH in cloud function, and how can I import this package.
Thanks in advance.
Run
go get -u -x google.golang.org/api/compute/v1
Then cd/open your directory in terminal then run
go mod init filename.go
then
go mod tidy
Also restart your IDE as that will need to update

Can't import packages

I'm trying to import a subdirectory I have in my project to my main file, but for some reason I get this error:
could not import ./service (no package for import ./service)
This is how I import:
import (
"os"
"service"
)
I also tried "service/app"
"./service/app"
"./service" nothing works, always the same error.
I'm trying to use the app.go file
This is the file structure:
Project Directory
-- main.go
-- service (directory)
-- app.go (file)
I tried to restart VS Code, tried to use go install/update tools, nothing works.
Also, this is my main func:
func main() {
a := &service.App()
a.Initialize(
os.Getenv("APP_DB_USERNAME"),
os.Getenv("APP_DB_PASSWORD"),
os.Getenv("APP_DB_NAME"),
)
a.Run(":8010")
}
&service.App() does not show a problem, but when I remove the "service" import, it does say
undeclared name: service
So I don't understand what the problem is.
This error sometime show on the "os" import too, I don't know why.
Golang doesn't support relative imports.
In modules, there finally is a name for the subdirectory. If the parent directory says "module m" then the subdirectory is imported as "m/subdir", no longer "./subdir".
So, in your case you should import service package as your_module_name/service.

How to import files from current directory in go

Intro
I'm trying to import my EventController.go to my main.go file.
Directory:
├───Controllers
│ └───Event
│ └───EventController.go
├───Models
├───Routes
│
└ Main.go
Problem:
import (
"log"
"net/http"
_ "/Controllers/Event/EventController.go" //problem here
)
error : cannot import absolute path
I read some documentation but the thing is I see that I'm doing it correctly, though i learnt about $GOPATH but I want to use the local directory thing.
What am I doing wrong and what's this error about
NOTE: I want add that I'm using windows as os
Thank you.
There are a few issues:
Packages are imported, not files (as noted in other answers)
File absolute import paths are not valid as the error states. An application can use file relative import paths (the path starts with "./") or a path relative to the Go workspace. See the go command doc for info on the syntax. Import paths relative to the Go workspace are the preferred form.
It is idiomatic to use lowercase names for packages (and their corresponding directories). The camel case names in the question are workable, but it's better to go with the flow.
The document How to Write Go Code is a nice tutorial on how to do this.
Here's how to reorganize the code given the above. This assumes that main.go is in a package with import path "myapp". Change this import path to whatever you want.
-- main.go --
package main
import (
"log"
_ "myapp/controllers/event"
)
func main() {
log.Println("hello from main")
}
-- go.mod --
module myapp
-- controllers/event/eventController.go --
package event
import "log"
func init() {
log.Println("hello from controllers/event")
}
Run this example on the Go playground.
You cannot import a file. You can import a package. So, lets say your main is the package "github.com/mypackage", then you should import "github.com/mypackage/Controllers/Event".
Go supports package level import. You can import a package by adding it to the import statement at the beginning of the file.
In your case, you should do something like this-
import (
"log"
"net/http"
"Controllers/Event/EventController"
)
Also, you should remove first "/" from the file name
_ /Controllers/Event/EventController.go" //problem here
because your Controllers folder is at the same level as Main.go file. You should always give relative path in the import statements.
In this way, you can use any file which is listed under EventController folder.

How to deploy Go apps with internal package to Heroku using `dep`

I am using Heroku's Golang buildpack to deploy a simple web app with the following structure
my-app/
handler/
user.go
session.go
vendor/
github.com/
golang.org/
main.go
Gopkg.toml
Gopkg.lock
Inside my main file, I imported my own handler package
import (
"fmt"
"net/http"
"my-app/handler"
)
Heroku was not able to run go install on my project due the following error:
----> Using go1.9.3
----> Running: go install -v -tags heroku .
----> cannot find package "my-app/handler" in any of: ...
I can run go install and my-app without any problem locally. It seems to me that heroku does not recognize my internal project package.
I am using dep and I have the following configurations in my Gopkg.toml:
[metadata.heroku]
root-package = "github.com/mygithub/my-app"
go-version = "go1.9.3"
install = ["."]
ensure = "false"
What else do I need to do to deploy a Go app with internal package? Thanks.
Try to set the root package to just the name of the project without the domain
[metadata.heroku]
root-package = "my-app"
go-version = "go1.9.2"
install = [ "./..." ]

Internal packages in Go

How to import internals packages in Go ?
import (
"runtime/internal/atomic"
"runtime/internal/sys"
)
Like this without get a error:
imports runtime/internal/atomic: use of internal package not allowed
And use internal funcs in a main package ?
Background
Go encourages structuring a program as a collection of packages interacting using exported APIs. However, all packages can be imported. This creates a tension when implementing a library or command: it may grow large enough to structure as multiple packages, but splitting it would export the API used in those additional packages to the world. Being able to create packages with restricted visibility would eliminate this tension.
A rule proposed to Go 1.4
An import of a path containing the element “internal” is disallowed if the importing code is outside the tree rooted at the parent of the “internal” directory.
Short answer
You can't (at least easily) and you shouldn't.
I will show you how I use internal nettest package:
// I copied nettest to vendor with `dep ensure` I think. Then:
mkdir vendor-local
cp -r vendor/golang.org/x/net/internal/nettest ./vendor-local/nettest
vim ./vendor-local/nettest/stack.go and remove import comment // import "foo" [1]
// Use full import in your go file:
import "github.com/foo-user/bar-project/vendor-local/nettest"
[1]: https://github.com/golang/net/blob/a8b9294777976932365dabb6640cf1468d95c70f/internal/nettest/stack.go#L6
Docs about import comments
You may find all import comments in your internal package with grep -r "// import" ./vendor-local/nettest
Why can't I copy nettest to ./vendor and use shorter import
You can, but utils like dep ensure that don't support local packages will purge your copy.

Resources