0

I am using shapely 1.8.2 and when I import it, I cannot use shape.wkt. For example, if I do:

import shapely
shapely.wkt

or

import shapely
shapely.wkt.loads(my_polygon)

I get AttributeError: module 'shapely' has no attribute 'wkt'.

But if I do

import shapely
from shapely import wkt

I can now access shapely.wkt. Can someone explain why this is happening? I would expect to be able to access shapely.wkt without directly importing wkt. Did I mess up something in my setup or is this how shapely is supposed to work?

jss367
  • 4,759
  • 14
  • 54
  • 76
  • It is not your fault. Shapely import structure is like that. – Msvstl Oct 23 '22 at 07:13
  • It comes down to the sometimes rather fuzzy [discinction between packages and modules](https://stackoverflow.com/questions/7948494/whats-the-difference-between-a-python-module-and-a-python-package). – Ture Pålsson Oct 23 '22 at 09:04

1 Answers1

0

This is a design decision by the developers of that package. What does or does not appear in the root package namespace comes down to what they put in the package’s __init__.py file.

Currently, there are lots of things imported into the global shapely namespace, but not wkt:

from .lib import GEOSException  # NOQA
from .lib import Geometry  # NOQA
from .lib import geos_version, geos_version_string  # NOQA
from .lib import geos_capi_version, geos_capi_version_string  # NOQA
from .errors import setup_signal_checks  # NOQA
from ._geometry import *  # NOQA
from .creation import *  # NOQA
from .constructive import *  # NOQA
from .predicates import *  # NOQA
from .measurement import *  # NOQA
from .set_operations import *  # NOQA
from .linear import *  # NOQA
from .coordinates import *  # NOQA
from .strtree import *  # NOQA
from .io import *  # NOQA

# Submodule always needs to be imported to ensure Geometry subclasses are registered
from shapely.geometry import (  # NOQA
    Point,
    LineString,
    Polygon,
    MultiPoint,
    MultiLineString,
    MultiPolygon,
    GeometryCollection,
    LinearRing,
)

from ._version import get_versions

__version__ = get_versions()["version"]
del get_versions

setup_signal_checks()

This type of pattern is common across many packages. You can access pandas.DataFrame directly even though DataFrame is actually defined in pandas.core, but for testing methods you still need to import pandas.testing. Scipy forces you to import most submodules directly, as each submodule is large and importing all by default would drag down performance.

If you want to know why shapely made these specific choices, you’ll have to ask the developers :)

Michael Delgado
  • 13,789
  • 3
  • 29
  • 54