geoarrow / geoarrow-python

Python implementation of the GeoArrow specification
http://geoarrow.org/geoarrow-python/
Apache License 2.0
66 stars 4 forks source link

ImportError: cannot import name 'types' from 'geoarrow' (unknown location) #55

Closed amanbagrecha closed 1 month ago

amanbagrecha commented 1 month ago

I am connecting to my postgres db using ibis framework as follows

import ibis
con = ibis.connect("postgres://postgres:password@localhost:5432/postgres")

This runs fine, but a warning stating

WARNING:  database "postgres" has a collation version mismatch
DETAIL:  The database was created using collation version 2.31, but the operating system provides version 2.35.
HINT:  Rebuild all objects in this database that use the default collation and run ALTER DATABASE postgres REFRESH COLLATION VERSION, or build PostgreSQL with the right library version.

Now when I connect to my table,

mytable = con1.table("my_table_name")

it results in ImportError from geoarrow.

{
    "name": "ImportError",
    "message": "cannot import name 'types' from 'geoarrow' (unknown location)",
    "stack": "---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
File ~/work/ibis/.venv/lib/python3.10/site-packages/IPython/core/formatters.py:711, in PlainTextFormatter.__call__(self, obj)
    704 stream = StringIO()
    705 printer = pretty.RepresentationPrinter(stream, self.verbose,
    706     self.max_width, self.newline,
    707     max_seq_length=self.max_seq_length,
    708     singleton_pprinters=self.singleton_printers,
    709     type_pprinters=self.type_printers,
    710     deferred_pprinters=self.deferred_printers)
--> 711 printer.pretty(obj)
    712 printer.flush()
    713 return stream.getvalue()

File ~/work/ibis/.venv/lib/python3.10/site-packages/IPython/lib/pretty.py:419, in RepresentationPrinter.pretty(self, obj)
    408                         return meth(obj, self, cycle)
    409                 if (
    410                     cls is not object
    411                     # check if cls defines __repr__
   (...)
    417                     and callable(_safe_getattr(cls, \"__repr__\", None))
    418                 ):
--> 419                     return _repr_pprint(obj, self, cycle)
    421     return _default_pprint(obj, self, cycle)
    422 finally:

File ~/work/ibis/.venv/lib/python3.10/site-packages/IPython/lib/pretty.py:787, in _repr_pprint(obj, p, cycle)
    785 \"\"\"A pprint that just redirects to the normal repr function.\"\"\"
    786 # Find newlines and replace them with p.break_()
--> 787 output = repr(obj)
    788 lines = output.splitlines()
    789 with p.group():

File ~/work/ibis/.venv/lib/python3.10/site-packages/ibis/expr/types/core.py:83, in Expr.__repr__(self)
     81 def __repr__(self) -> str:
     82     if ibis.options.interactive:
---> 83         return _capture_rich_renderable(self)
     84     else:
     85         return self._noninteractive_repr()

File ~/work/ibis/.venv/lib/python3.10/site-packages/ibis/expr/types/core.py:63, in _capture_rich_renderable(renderable)
     61 console = Console(force_terminal=False)
     62 with console.capture() as capture:
---> 63     console.print(renderable)
     64 return capture.get().rstrip()

File ~/work/ibis/.venv/lib/python3.10/site-packages/rich/console.py:1710, in Console.print(self, sep, end, style, justify, overflow, no_wrap, emoji, markup, highlight, width, height, crop, soft_wrap, new_line_start, *objects)
   1708 if style is None:
   1709     for renderable in renderables:
-> 1710         extend(render(renderable, render_options))
   1711 else:
   1712     for renderable in renderables:

File ~/work/ibis/.venv/lib/python3.10/site-packages/rich/console.py:1311, in Console.render(self, renderable, options)
   1309 renderable = rich_cast(renderable)
   1310 if hasattr(renderable, \"__rich_console__\") and not isclass(renderable):
-> 1311     render_iterable = renderable.__rich_console__(self, _options)  # type: ignore[union-attr]
   1312 elif isinstance(renderable, str):
   1313     text_renderable = self.render_str(
   1314         renderable, highlight=_options.highlight, markup=_options.markup
   1315     )

File ~/work/ibis/.venv/lib/python3.10/site-packages/ibis/expr/types/core.py:106, in Expr.__rich_console__(self, console, options)
    103 if opts.interactive:
    104     from ibis.expr.types.pretty import to_rich
--> 106     rich_object = to_rich(self, console_width=console_width)
    107 else:
    108     rich_object = Text(self._noninteractive_repr())

File ~/work/ibis/.venv/lib/python3.10/site-packages/ibis/expr/types/pretty.py:274, in to_rich(expr, max_rows, max_columns, max_length, max_string, max_depth, console_width)
    270     return _to_rich_scalar(
    271         expr, max_length=max_length, max_string=max_string, max_depth=max_depth
    272     )
    273 else:
--> 274     return _to_rich_table(
    275         expr,
    276         max_rows=max_rows,
    277         max_columns=max_columns,
    278         max_length=max_length,
    279         max_string=max_string,
    280         max_depth=max_depth,
    281         console_width=console_width,
    282     )

File ~/work/ibis/.venv/lib/python3.10/site-packages/ibis/expr/types/pretty.py:347, in _to_rich_table(tablish, max_rows, max_columns, max_length, max_string, max_depth, console_width)
    344     if orig_ncols > len(computed_cols):
    345         table = table.select(*computed_cols)
--> 347 result = table.limit(max_rows + 1).to_pyarrow()
    348 # Now format the columns in order, stopping if the console width would
    349 # be exceeded.
    350 col_info = []

File ~/work/ibis/.venv/lib/python3.10/site-packages/ibis/expr/types/core.py:489, in Expr.to_pyarrow(self, params, limit, **kwargs)
    461 @experimental
    462 def to_pyarrow(
    463     self,
   (...)
    467     **kwargs: Any,
    468 ) -> pa.Table:
    469     \"\"\"Execute expression and return results in as a pyarrow table.
    470 
    471     This method is eager and will execute the associated expression
   (...)
    487         A pyarrow table holding the results of the executed expression.
    488     \"\"\"
--> 489     return self._find_backend(use_default=True).to_pyarrow(
    490         self, params=params, limit=limit, **kwargs
    491     )

File ~/work/ibis/.venv/lib/python3.10/site-packages/ibis/backends/__init__.py:216, in _FileIOHandler.to_pyarrow(self, expr, params, limit, **kwargs)
    214 table_expr = expr.as_table()
    215 schema = table_expr.schema()
--> 216 arrow_schema = schema.to_pyarrow()
    217 with self.to_pyarrow_batches(
    218     table_expr, params=params, limit=limit, **kwargs
    219 ) as reader:
    220     table = pa.Table.from_batches(reader, schema=arrow_schema)

File ~/work/ibis/.venv/lib/python3.10/site-packages/ibis/expr/schema.py:190, in Schema.to_pyarrow(self)
    187 \"\"\"Return the equivalent pyarrow schema.\"\"\"
    188 from ibis.formats.pyarrow import PyArrowSchema
--> 190 return PyArrowSchema.from_ibis(self)

File ~/work/ibis/.venv/lib/python3.10/site-packages/ibis/formats/pyarrow.py:246, in PyArrowSchema.from_ibis(cls, schema)
    243 @classmethod
    244 def from_ibis(cls, schema: Schema) -> pa.Schema:
    245     \"\"\"Convert a schema to a pyarrow schema.\"\"\"
--> 246     fields = [
    247         pa.field(name, PyArrowType.from_ibis(dtype), nullable=dtype.nullable)
    248         for name, dtype in schema.items()
    249     ]
    250     return pa.schema(fields)

File ~/work/ibis/.venv/lib/python3.10/site-packages/ibis/formats/pyarrow.py:247, in <listcomp>(.0)
    243 @classmethod
    244 def from_ibis(cls, schema: Schema) -> pa.Schema:
    245     \"\"\"Convert a schema to a pyarrow schema.\"\"\"
    246     fields = [
--> 247         pa.field(name, PyArrowType.from_ibis(dtype), nullable=dtype.nullable)
    248         for name, dtype in schema.items()
    249     ]
    250     return pa.schema(fields)

File ~/work/ibis/.venv/lib/python3.10/site-packages/ibis/formats/pyarrow.py:211, in PyArrowType.from_ibis(cls, dtype)
    209     return pa.map_(key_field, value_field, keys_sorted=False)
    210 elif dtype.is_geospatial():
--> 211     from geoarrow import types as gat
    213     # Resolve CRS
    214     if dtype.srid is None:

ImportError: cannot import name 'types' from 'geoarrow' (unknown location)"
}

I tried installing the latest build as well from the documentation and also changed the python version to 3.11 and now 3.10, but to no avail, the error persists. Would appriciate any help.

Many thanks

kylebarron commented 1 month ago

You need to pip install geoarrow-types https://pypi.org/project/geoarrow-types/

(That said, I'm not sure why the error exists. Some library isn't declaring its geoarrow-types dependency, I suppose?)

amanbagrecha commented 1 month ago

Thanks, it was also because I had to install pip install 'ibis-framework[postgres]', which resolved the issue. I had previously only ran ibis-framework previously.

Thanks Kyle!