2

I'm using geopy and have a question on why an error is coming up.

This code sample is from the one provided at github. It works as mentioned

from geopy.geocoders import Nominatim

geolocator = Nominatim()

location = geo.geocode("NY")

print((location.latitude, location.longitude))

How come the code below provides an error? What's the reason behind it?

from geopy.geocoders import Nominatim as geo

location = geo.geocode("NY")

print((location.latitude, location.longitude))

The error provided by the second code is:

Traceback (most recent call last):
  File "C:/Users/Leb/Desktop/Python/so2.py", line 5, in <module>
    location = geo.geocode("NY")
TypeError: geocode() missing 1 required positional argument: 'query'
Himanshu Mishra
  • 8,510
  • 12
  • 37
  • 74
Leb
  • 15,483
  • 10
  • 56
  • 75
  • I think this [answer](http://stackoverflow.com/a/17534363/3337714) might resolve your query. – user3337714 Jun 21 '15 at 20:29
  • 1
    in the first code sample you create object Nomination, then in second sample you don't, so it expects two arguments from you `self` and some string I believe. And you are passing only one. – ThePavolC Jun 21 '15 at 20:29
  • Thank you, that explains the reason quite well. – Leb Jun 21 '15 at 20:34

1 Answers1

1

You need to instantiate class (create object)

from geopy.geocoders import Nominatim as geo

location = geo().geocode("NY")

print((location.latitude, location.longitude))
ThePavolC
  • 1,693
  • 1
  • 16
  • 26
  • @ThePavolIC did you just copy SukritKalra answer's from the comment? Interesting! – user3337714 Jun 21 '15 at 20:33
  • Not really, you have posted a link to answer. I think I gave the answer. But I didn't refresh the page while writing comment so I didn't see other posts – ThePavolC Jun 21 '15 at 20:35
  • That means that the question is duplicate and it already has a answer. You are providing repeating solutions instead of re-directing?. – user3337714 Jun 21 '15 at 20:36