geopy TypeError : geocode() missing 1 required positional argument: 'query'

3.2k views Asked by At

I am sorry if this is a too basic question however I am unable to understand a simple error that geopy is throwing at me.

In [78]: import geopy

In [79]: geopy.geocoders.Nominatim.geocode("Mumbai")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-79-2465009b9d72> in <module>()
----> 1 geopy.geocoders.Nominatim.geocode("Mumbai")

TypeError: geocode() missing 1 required positional argument: 'query'

However, the following works and I am not getting how these two approaches are different:

In [83]: from geopy.geocoders import Nominatim

In [84]: geolocator = Nominatim()

In [85]: geolocator.geocode("Mumbai")
Out[85]: Location(Mumbai, Greater Bombay, Maharashtra, India, (18.9321862, 72.8308337, 0.0))

As far as I can see, both seem to be equivalent. What am I missing?

1

There are 1 answers

0
alecxe On BEST ANSWER

The problem is that in the first case, you don't create an instance of the Nominatum class and try to call a geocode() instance method with a single argument. Since there is no instance, Mumbai is used as the self positional argument value leaving the required query argument not-specified - hence the error.

The both options would be equivalent if geocode() would be a "class method" instead of being an "instance method" (reference).