unioslo / harborapi

Python async client for the Harbor REST API v2.0.
https://unioslo.github.io/harborapi/
MIT License
28 stars 5 forks source link

ImportError: cannot import name 'HarborAsyncClient' from partially initialized module 'harborapi' #41

Closed nikolasj closed 1 year ago

nikolasj commented 1 year ago

Hello, I use the last version of the package. I get an error: ImportError: cannot import name 'HarborAsyncClient' from partially initialized module 'harborapi'

pederhan commented 1 year ago

Can you post the traceback and/or the code you are trying to run? I am able to import HarborAsyncClient without any issues:

from harborapi import HarborAsyncClient

client = HarborAsyncClient(
    # ...
)
nikolasj commented 1 year ago

install harborapi (last version) pip3 install harborapi (I use python3.9)

from harborapi import HarborAsyncClient
import asyncio

client = HarborAsyncClient(
    url="registry",
    username="username",
    secret="secret",
)

async def main() -> None:
    # Get all projects
    projects = await client.get_projects()
    for project in projects:
        print(project.name)

    # If you have rich installed:
    import rich

    for project in projects:
        rich.print(project)

asyncio.run(main())
Traceback (most recent call last):
  File "/home/nikolai/src/harborapi.py", line 1, in <module>
    from harborapi import HarborAsyncClient
  File "/home/nikolai/src/harborapi.py", line 1, in <module>
    from harborapi import HarborAsyncClient
ImportError: cannot import name 'HarborAsyncClient' from partially initialized module 'harborapi' (most likely due to a circular import)
pederhan commented 1 year ago

It's because you are running from a file called harborapi.py, and the line from harborapi import HarborAsyncClient is resolved by the Python import mechanism as the local file called harborapi.py as opposed to the installed module harborapi.

I tested, and got the same error as you when I named my file harborapi.py. Change the name of the file to something like main.py, and it should work.

nikolasj commented 1 year ago

Thank you!