Trying to use the grafeas python client library

128 views
Skip to first unread message

Dan Chernoff

unread,
Feb 15, 2021, 2:35:49 PM2/15/21
to Grafeas Users
Trying to use a grafeas python client library.. not in the docs on connecting to a locally running instance and putting/getting metadata. Any recommendations on examples (link to the client library I am using below).

https://googleapis.dev/python/grafeas/latest/index.html

Wiktor Kozlik

unread,
Feb 16, 2021, 1:30:58 PM2/16/21
to Grafeas Users
We just updated https://github.com/grafeas/client-python. Is this what you are looking for?

Dan Chernoff

unread,
Feb 23, 2021, 3:33:28 PM2/23/21
to Grafeas Users
My apologies for the delay in getting back to you. I looked at the python package you linked to as well. 

I cloned the master branch for the repository (https://github.com/grafeas/client-python.git) and did the install using setup (python3 setup.py install --user).  I also ran the install via pip3 as described in the readme. I get the error at the bottom of the page when trying to run the sample from the getting started. 

Thanks in advance for your help.

Dan

OS and Python Version
-----------------------------------------------------
python3 --version
Python 3.8.5

Install and Output:
------------------------------------------------------
Requirement already satisfied: Grafeas from git+https://github.com/grafeas/client-python.git#egg=Grafeas in /home/kilmore/.local/lib/python3.8/site-packages/Grafeas-1a1-py3.8.egg (1a1)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from Grafeas) (2019.11.28)
Requirement already satisfied: python-dateutil>=2.1 in /usr/lib/python3/dist-packages (from Grafeas) (2.7.3)
Requirement already satisfied: six>=1.10 in /usr/lib/python3/dist-packages (from Grafeas) (1.14.0)
Requirement already satisfied: urllib3>=1.23 in /usr/lib/python3/dist-packages (from Grafeas) (1.25.8)


Error:
-------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    import grafeas
  File "/home/kilmore/code/experiments/grafeas/grafeas.py", line 17, in <module>
    from grafeas.grafeas_v1.gapic import enums
ModuleNotFoundError: No module named 'grafeas.grafeas_v1'; 'grafeas' is not a package

Brian Costlow

unread,
Mar 1, 2021, 8:35:51 PM3/1/21
to Grafeas Users
Is this file /home/kilmore/code/experiments/grafeas/grafeas.py code that you wrote? 

It's possible that file is interfering with the import machinery if it ends up on the Python path, which if test.py is in the same location as grafeas.py it certainly will.
In general, it's not a good idea to name any of your own project files the same as any packages/modules you will be installing and importing. If you made those yourself, change the names. 

Also, the instructions for installing are more for a dev setup than just using grafeas. For just installing the module to use it, I recommend installing into a virtual environment for full package isolation. Note once you are using the environment, you can just use python, pip etc. You can omit the '3'


python3 -m venv graftest
graftest/bin/python -m pip install --upgrade pip
source graftest/bin/activate
python
> import grafeas
>

Dan Chernoff

unread,
Mar 9, 2021, 8:25:52 PM3/9/21
to Grafeas Users
Thank you for the guidance. I am still struggling with using the client to connect to a locally running grafeas database. 

I run the database using -  docker run -p 8080:8080 --name grafeas2  us.gcr.io/grafeas/grafeas-server:v0.1.0

Once the database is up I can curl grafeas using  -- curl http://localhost:8080/v1beta1/projects -- and get a response.  However, when I create a client connection using the code below I get a 404.  It is connecting to the locally running grafeas (I get a different error message when the container is stopped). 

It looks like the default resource_path is v1alpha1  and the current grafeas container uses v1beta1.  I can update the setup.py /config.py to v1beta1 and re-install but my greater concern is that there are other changes beyond the grafeas endpoint.   Is there a new version of the client we should use? If there is not, do we muddle through and issue PR's to the client. Relevant issue post on github ( https://github.com/grafeas/client-python/issues/9 ) .

Thanks in advance for all your help!

Dan

##########################################################################

Response when using curl
--------------------------------------------------------
{"projects":[],"nextPageToken":""}

Response when using python code
-----------------------------------------------------
Exception when calling GrafeasApi->create_note: (404)
Reason: Not Found
HTTP response headers: HTTPHeaderDict({'Content-Type': 'text/plain; charset=utf-8', 'Vary': 'Origin', 'X-Content-Type-Options': 'nosniff', 'Date': 'Wed, 10 Mar 2021 01:08:45 GMT', 'Content-Length': '10'})
HTTP response body: Not Found

Code
-----------------------------------------------------
from __future__ import print_function
import time
import grafeas
from grafeas.rest import ApiException
from grafeas.api_client import ApiClient
from pprint import pprint
from grafeas.configuration import Configuration

# Create the client connection to the locally running DB
connection = Configuration()
connection.host = "http://localhost:8080"
connection.verify_ssl = False

# Create the api_client connection
api_client = ApiClient(configuration=connection)

# Create the api projects instance
api_projects_instance = grafeas.GrafeasProjectsApi(api_client=api_client)


try:

api_list_response = api_projects_instance.list_projects()
pprint(api_list_response)

except ApiException as e:
print("Exception when calling GrafeasApi->create_note: %s\n" % e)

Brian Costlow

unread,
Mar 9, 2021, 8:50:16 PM3/9/21
to Grafeas Users
I have about as much experience with this as you, maybe less.

Yup, the API which is auto-generated, is still on v1alpha1 for the Python client, and the server has been on v1beta1 for some time. Looks like somebody recently regenerated the Python API with a newer swagger tool and updated some of the api endpoints but still for the v1aplha1 version. 

I opened an issue, and am planning to work on the v1beta1 migration, and submit a PR (unless somebody else gets there first). But due to work and another Python community commitment, it will be a couple weeks until I can even get started.

--
You received this message because you are subscribed to a topic in the Google Groups "Grafeas Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grafeas-users/vhvHWdWDe_M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to grafeas-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grafeas-users/2f63181a-f208-4c14-87fe-183a0b7835acn%40googlegroups.com.

Dan Chernoff

unread,
Mar 9, 2021, 9:41:15 PM3/9/21
to Grafeas Users
Appreciate the fast feedback. I have some cycles and would love to help out.  I have never used the swagger codegen, but I’d love to learn/update the client. 

What is the best way to come up to speed and get involved?

Dan

Brian Costlow

unread,
Mar 10, 2021, 4:28:58 PM3/10/21
to Grafeas Users
You got lucky on fast feedback, I just happened to be reading email when your message arrived.

I'm still feeling my way around myself, and basically trying to fill a possible need; we're evaluating using Grafeas server and the Go client for a project, but if we do, also having the Python one working would be useful.

There's a shell script that regenerates the API. You can adjust what's in it and see what happens when you try to rebuild....


You should be able to change line 5 to the current protocol definitions, and optionally change line 7 to get a later version of the cli jar, looks like the 2.x version is now on 2.4.19, and run it.

Dan Chernoff

unread,
Mar 11, 2021, 11:05:24 AM3/11/21
to Brian Costlow, Grafeas Users
THANKS! I'll look at it tonight.

Based on the release notes for grafeas there were changes to methods. Will the rebuild via swagger capture those? (https://github.com/grafeas/grafeas/blob/master/docs/release_notes.md)

Dan



--
--
With Great Power, Comes Great Complexity....

Brian Costlow

unread,
Mar 17, 2021, 8:19:57 AM3/17/21
to Grafeas Users

Sorry, just been real busy. On the client side, I think the generated code is sufficient (it has been when I've done this for other projects). But I have not done anything more than a superficial glance. 

IIRC, there are no automated tests, so I would poke at the newly generated code pretty rigorously.  I should be able to spend some time of this over the upcoming weekend.

Dan Chernoff

unread,
Mar 17, 2021, 10:13:52 AM3/17/21
to Brian Costlow, Grafeas Users
Thank you for the update!

I am going to carve out some time this weekend as well.

You mentioned you are looking at the GoLang Grafeas library. Out of curiosity, what state is it in?


Dan

Reply all
Reply to author
Forward
0 new messages