cannot import name GoogleMaps in python

13,577

Solution 1

Use geopy instead, no need for api-key.

From their example:

from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("175 5th Avenue NYC")
print(location.address)
print((location.latitude, location.longitude))

prints:

Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, 10010,  United States of America
(40.7410861, -73.9896297241625)

Solution 2

I think what you are looking for is the Client class not GoogleMaps.

If you want to call it GoogleMaps import it as follows:

from googlemaps import Client as GoogleMaps

Solution 3

Another option is parsing the json from photon.komoot.de. Example:

import requests, json

url = 'http://photon.komoot.de/api/?q='
addresses = ['175 5th Avenue NYC', 'Constitution Ave NW & 10th St NW, Washington, DC']

for address in addresses:
    resp = requests.get(url=url+address)
    data = json.loads(resp.text)
    print data['features'][0]['geometry']['coordinates']

prints:

[-76.1438449, 40.229888]
[-77.046567, 38.8924587]

These are given in lon, lat. The second one is a bit off by about 1 mile. Seems street intersections are difficult.

Solution 4

I could write a code for multiple address but it never worked.. Finally found this website which could generate geocodes in bulk.. I think it may be useful to someone looking for bulk geocodes.. It also has reverse geocoding..

http://www.findlatitudeandlongitude.com/batch-geocode/#.VW2KRs-qqkq

Solution 5

Andrzej's answer worked for me!

from googlemaps import Client as GoogleMaps

Just to add to what he said, you will need to

  • Click on the API Library in your Google Cloud Platform console
  • Search for "Geolocation API"
  • Click on "Enable"

If you create an API key before enabling this, you will also get that error. I had the same issue.

Under the "Credentials" tab, you will then see the option to create or copy your API key.

Share:
13,577

Related videos on Youtube

Pavan Chakravarthy
Author by

Pavan Chakravarthy

Updated on September 16, 2022

Comments

  • Pavan Chakravarthy
    Pavan Chakravarthy about 1 year

    I am using the code below to get the latitude & longitude of an address:

    from googlemaps import GoogleMaps
    gmaps = GoogleMaps(api_key)
    address = 'Constitution Ave NW & 10th St NW, Washington, DC'
    lat, lng = gmaps.address_to_latlng(address)
    print lat, lng
    

    but am getting the error below

    File "C:/Users/Pavan/PycharmProjects/MGCW/latlong6.py", line 1, in <module>
        from googlemaps import GoogleMaps
    ImportError: cannot import name GoogleMaps
    

    I have seen another question similar to this, but the solution didn't work for me.

  • Pavan Chakravarthy
    Pavan Chakravarthy over 8 years
    did that & now am getting this error File "C:/Users/Pavan/PycharmProjects/MGCW/latlong6.py", line 2, in <module> gmaps = GoogleMaps(api_key) NameError: name 'api_key' is not defined
  • Scott
    Scott over 8 years
    @PavanChakravarthy that's because you need an api. Use geopy instead.
  • Andrzej Pronobis
    Andrzej Pronobis over 8 years
    Did you define the name api_key? It seems that you didn't.
  • Pavan Chakravarthy
    Pavan Chakravarthy over 8 years
    this code is working fine for single location & when am passing multiple locations am getting all latitudes & longitudes same..
  • Andrzej Pronobis
    Andrzej Pronobis over 8 years
    Check this out for a description of the API key: github.com/googlemaps/google-maps-services-python#api-keys
  • Pavan Chakravarthy
    Pavan Chakravarthy over 8 years
    this seems to work now.. anyhow I need to locate 44,000 locations.. will try to put them in a file & will pass them..
  • Scott
    Scott over 8 years
    @PavanChakravarthy I'm not too familiar with the library but they do seem to have nice documentation geopy.readthedocs.org/en/1.10.0
  • Pavan Chakravarthy
    Pavan Chakravarthy over 8 years
    as expected the above code worked fine for few locations & if it couldnt find the coordinates it would fail other locations also..
  • Scott
    Scott over 8 years
    @PavanChakravarthy Yes geopy seems to not be able to handle some types of addresses. Check out my other answer using photon.komoot.de.
  • Brian
    Brian about 5 years
    @Scott slightly off topic, but I am wondering if you know of a way to generate lat longs from zip codes? I am having some trouble locating a good solution for python, thank you!
  • Scott
    Scott about 5 years
    @Brian I have had to do that before. You basically need to know the center or some representative lat/lon for the whole zip code. Different sites have compiled extensive lists in csv format from government data. Read it in perhaps as a dictionary with key and zip code and value as object/named tuple that contains lat/lon. Top link right now on google is gist.github.com/erichurst/7882666 containing data from 2013.
  • Brian
    Brian about 5 years
    @Scott thanks for the reply. The US ones aren't as big of a pain to handle. I am trying to do this across the world for ~1.2 million zip to zip combinations. I am trying to avoid paying for an API. However it doesn't look like any packages accomplish this at scale.
  • Scott
    Scott about 5 years
    @Brian Sorry I don't know of any free service with worldwide data.
  • Mostafa Ghadimi
    Mostafa Ghadimi about 4 years
    Your answer is not true. The problem is from the package, not the installation!
  • Admin
    Admin almost 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.