how to specify the name of a package? - python-packaging

I have built and installed via pip a python package, let's say tetris. Now, on a another branch I have written code that adds some new functionality to the master branch, lets call that branch tetris_3D.
I want to build and install on my local pc a package from the tetris_3D branch and give it a name tetris_3D. I would like both packages tetris and tetris_3D to co-exist on my machine.
How can I do that?
I thought that changing the name in the setup.py file would work. However something like:
setup(
name="tetris_3D",
license="BSD",
...
...
doesnt seem to help. The whl is getting created, the package is installed but I cannot import it. My Anaconda3\Lib\site-packages folder has both tetris_3D.dist.info and tetris folders but it looks to me that the 3D code has overwritten everything inside tetris instead of creating a new fresh one.
When I do import tetris then it is the 3D code that gets imported.

Related

How do I use pyinstaller to package a large multi-folder project?

Here is an example project folder structure similar to my actual project.
-repo_folder
--app_folder
---GUI_folder
----GUI1.py
----GUI2.py
---calculations_folder
----calculations1.py
----calculations2.py
---main.py
--cli.py
cli.py points to main.py. Main.py only import GUI1.py. And from there GUI2.py is imported and so on.
Basically, is there an easy way to make sure that all the importing done in each file is included? I have not been able to succesfully export a project with this kind of folder setup succesfully using pyinstaller. I keep getting "Failed to execute script cli" or "No module named GUI".
Could someone make an example of how the code would be import for a project structured like the above?

How can I import custom modules from a Github repository in Google Colab?

I understand how to run a single notebook in Colab. However, I am not sure how to use all files from a repository, i.e to be able to import functions inside Colab notebook?
Thank you.
Let's say we want to run the ipynb file, named as "1-fully-connected-binarized-mnist" residing in the repo "qnn-inference-examples".
https://github.com/maltanar/qnn-inference-examples
The notebook of interest uses customly created QNN library and functions inside that repo. Yes we need to import that function. To do this, we should first upload the repo folder to Google Colab, then correct/modify library and file paths.
0) Open the ipynb file "1-fully-connected-binarized-mnist" on your Colab. You can rename it if you like.
Try to run it, but will probably get some errors (as I did). So let's fix these issues
1) Insert a new code cell at the top of the notebook. And clone the repo on your Colab:
!git clone https://github.com/maltanar/qnn-inference-examples.git
now the new folder "qnn-inference-examples" created under your "content" folder. you should see something like this on the left side. And remember the path "/content/qnn-inference-examples"
2) Now add the second new cell on top:
import sys
sys.path.insert(0,'/content/qnn-inference-examples')
This will fix the issue about not able to find the library location, when trying import the QNN libraries.
3) Manually fix the file links on the existing code, according to the new path. Because the library and files now exist under the folder "/content/qnn-inference-examples":
for example replace:
img = Image.open("7.png")
with
img = Image.open("/content/qnn-inference-examples/7.png")
These steps should do the work
Please note that: This is not my own solution, mix of 2 or 3 solutions. Credit goes to Hüseyin Elçi, KDnuggets and Alexandr Haymin
https://medium.com/analytics-vidhya/importing-your-own-python-module-or-python-file-into-colab-3e365f0a35ec
https://www.kdnuggets.com/2018/02/google-colab-free-gpu-tutorial-tensorflow-keras-pytorch.html/2
Please see the example below:
!git clone https://www.github.com/matterport/Mask_RCNN.git
from google.colab import files
files.os.chdir('Mask_RCNN')
# To find local version of the library
sys.path.append(os.path.join(ROOT_DIR, 'Mask_RCNN'))
# here is your import
from mrcnn.config import Config

Golang Importing Issue

I'm trying to use import a package for internal use, but I have been having some issues.
My directory structure looks like this:
app/
model/
file1.go
file2.go
...
main.go
When I try to build the program, I get an error that looks something like this:
/usr/local/go/src/pkg/model (from $GOROOT)
I want to be able to call the model programs in any of my other programs in the app simply using:
import "app/model"
What are my options when it comes to doing this?
You import from GOPATH level .. all of your packages should live there.
For example, assuming your application is here:
$GOPATH/src/dtrinh100/app/
..and your package you wish to import is here:
$GOPATH/src/github.com/other/package
Your import would be:
import "github.com/other/package"
You should review the literature around what the GOPATH environment variable is all about. When beginning Go, it is important you understand its purpose and initially, you should place all of your projects/packages inside of the GOPATH.
When you import a custom package, Go looks for its definition in each workspace listed in the GOPATH environment variable. Your custom package should be defined in a src subdirectory.
If you keep your code in a source repository somewhere, then you should use the root of that source repository as your base path. For instance, if you have a GitHub account at github.com/user, that should be your base path.
Note that you don't need to publish your code to a remote repository before you can build it. It's just a good habit to organize your code as if you will publish it someday. In practice you can choose any arbitrary path name, as long as it is unique to the standard library and greater Go ecosystem.
You should use github.com/user as our base path. Create a directory inside your workspace in which to keep source code:
$ mkdir -p $GOPATH/src/github.com/user
You can look at How to Write Go Code for more details.

importing go files in same folder

I am having difficulty in importing a local go file into another go file.
My project structure is like something below
-samplego
--pkg
--src
---github.com
----xxxx
-----a.go
-----b.go
--bin
I am trying to import a.go inside b.go. I tried the following,
import "a"
import "github.com/xxxx/a"
None of these worked..I understand I have to meddle up with GOPATH but I couldn't get it right. Presently my GOPATH is pointing to samplego(/workspace/samplego).I get the below error
cannot find package "a" in any of:
/usr/local/go/src/pkg/a (from $GOROOT)
/workspace/samplego/src/a (from $GOPATH)
Also, how does GOPATH work when these source files are imported into another project/module? Would the local imports be an issue then? What is the best practice in this case - is it to have just one go file in module(with associated tests)?
Any number of files in a directory are a single package; symbols declared in one file are available to the others without any imports or qualifiers. All of the files do need the same package foo declaration at the top (or you'll get an error from go build).
You do need GOPATH set to the directory where your pkg, src, and bin directories reside. This is just a matter of preference, but it's common to have a single workspace for all your apps (sometimes $HOME), not one per app.
Normally a Github path would be github.com/username/reponame (not just github.com/xxxx). So if you want to have main and another package, you may end up doing something under workspace/src like
github.com/
username/
reponame/
main.go // package main, importing "github.com/username/reponame/b"
b/
b.go // package b
Note you always import with the full github.com/... path: relative imports aren't allowed in a workspace. If you get tired of typing paths, use goimports. If you were getting by with go run, it's time to switch to go build: run deals poorly with multiple-file mains and I didn't bother to test but heard (from Dave Cheney here) go run doesn't rebuild dirty dependencies.
Sounds like you've at least tried to set GOPATH to the right thing, so if you're still stuck, maybe include exactly how you set the environment variable (the command, etc.) and what command you ran and what error happened. Here are instructions on how to set it (and make the setting persistent) under Linux/UNIX and here is the Go team's advice on workspace setup. Maybe neither helps, but take a look and at least point to which part confuses you if you're confused.
No import is necessary as long as you declare both a.go and b.go to be in the same package. Then, you can use go run to recognize multiple files with:
$ go run a.go b.go
./main.go (in package main)
./a/a.go (in package a)
./a/b.go (in package a)
in this case:
main.go import "./a"
It can call the function in the a.go and b.go,that with first letter caps on.
If none of the above answers works,
Just try,
go run .
for production,
go build
This will take care of all the .go files in the folder.
I just wanted something really basic to move some files out of the main folder, like user2889485's reply, but his specific answer didnt work for me. I didnt care if they were in the same package or not.
My GOPATH workspace is c:\work\go and under that I have
/src/pg/main.go (package main)
/src/pg/dbtypes.go (pakage dbtypes)
in main.go I import "/pg/dbtypes"
As people mentioned previously, there is no need to use any imports.
A lot of people mention that using go run is possibe when you mention most files, however when having multiple .go-files in the same dir it can be cumbersome.
Therefore using go run *.go is what I usually do.
As I understand for packages in your project subfolders it's possible now to do, just need to add "." in front of module, like
. "github.com/ilyasf/deadlock-train/common"
where github.com/ilyasf/deadlock-train my main module name and common is just package from /common subfolder inside project.
go1.19.1 version here. I've just had the same issue, discovered that you must simply do: import (a "github.com/xxxx")
You can go to the correct folder and execute: $ go run *.go
As long as the code only 1 main function in all files it works perfect!

Go, Golang : external package import with GOROOT

Go, Golang : does not make sense that I have to have files before import
I am trying to go to next step but keep getting errors
I have the package that I want to import ready.
All I need to do is to import the external package from github and be able to use it on any code.
So this is what I did.
mkdir $HOME/go
export GOPATH=$HOME/go
go get github.com/user/project
This runs successfully. I downloaded it onto here with source code files from github
/Users/user/go/src/github.com/user/project/project.go
So to use this package that I just import I do
go run /Users/user/Desktop/code.go
But I am getting the following errors
MacBook-Air:~ user$ go run /Users/user/Desktop/code.go
Desktop/code.go:32:8: cannot find package "project" in any of:
/usr/local/go/src/pkg/project (from $GOROOT)
/Users/user/go/src/project (from $GOPATH)
What should I do? AM I missing something? Thanks in advance and please help me. I wrote a lot of code but being very frustrated not being able to distribute it because of this.
The error message says at line 32 in your code.go it can't find package "goling".
Assuming that is a local package you want to use, you need to make sure it is in your GOPATH.
If you set GOPATH then you should develop your code within it, so moving the "goling" directory into /Users/user/go/src is the right thing to do.
Alternatively "goling" could be a typo, so check the imports in code.go. If you want to import an project from github the import should say
import "github.com/user/project"
And you then use the parts of project with a prefix of project.
If that doesn't help you get it working, then post the imports section of code.go.
It looks like you've got the external package in the same folder as your main package which uses it. In go, all packages must be in separate directories. It looks like the github project itself is actually doing that. If you separate the packages into different directories it should work properly.