简体   繁体   中英

ModuleNotFoundError: No module named 'googlemaps'

I am a newcomer to Django python programming. Now I am working on the server side. I want to use google maps API, my view.py is like this:

from django.shortcuts import render
from django.shortcuts import HttpResponse
from googlemaps import *
# Create your views
gmaps = googlemaps.Client(key='A')

def index(request):
    if request.method=="GET":
        geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
        return geocode_result

Also, I have already installed 'googlemaps' using pip. And when I import it in IDE, nothing goes wrong. But when I want to start the server to test the code, the server won't run and tells me ModuleNotFoundError: No module named 'googlemaps', I am confused that I have already downloaded it and added it in settings.py, importing in IDE also seems fine. But where I did wrong makes it fail to start the server?

I got it working two different ways (assuming valid API key). Either:

from googlemaps import Client
gmaps = Client(key='A')

or:

from googlemaps import *
gmaps = Client(key='A')

Do either of these work for you?

If you're still having problems, there's a good chance it's related to your virtualenv (if using). Try running:

pip freeze

from command line and searching for the requisite library.

If you don't have an API key, you may of course want to consider using geopy. Last I checked, didn't require API key.

from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("175 5th Avenue NYC")

Change from googlemaps import * to import googlemaps

What from googlemaps import * does is that it imports all the contents of the googlemaps module. import googlemaps imports the whole googlemaps module as a whole.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM