Django开发已安装googlemaps仍报ModuleNotFoundError问题求助
Hey there, let’s troubleshoot that ModuleNotFoundError for your Django + Google Maps setup step by step! It’s super common to hit this kind of issue when working with virtual environments and third-party libraries, so let’s break down the most likely fixes:
1. Double-check your virtual environment activation
Most Django projects use a virtual environment (like venv or virtualenv) to isolate dependencies. If you installed googlemaps while your project’s virtual environment wasn’t active, the library got installed to your global Python environment instead of the one your Django server is using.
To verify:
- Activate your project’s virtual environment:
- Linux/macOS:
source venv/bin/activate - Windows:
venv\Scripts\activate
- Linux/macOS:
- Run
pip listin the activated environment. Ifgooglemapsisn’t listed, reinstall it withpip install googlemapswhile the env is active.
2. Confirm your Django server uses the right Python interpreter
Sometimes your IDE might be configured to use one Python version, but your terminal (where you run python manage.py runserver) uses another. This leads to libraries being installed in one place but the server looking in another.
To fix this:
- In your terminal, run
which python(Linux/macOS) orwhere python(Windows) to get the path of the Python interpreter your server uses. - Install
googlemapsdirectly to that interpreter with:/path/to/your/python -m pip install googlemaps - You can also add a quick debug line to your
settings.pyto confirm the interpreter path when the server starts:
Run the server and check the terminal output for the path, then use that to install the library.import sys print("Python executable path:", sys.executable)
3. Remove googlemaps from INSTALLED_APPS (it doesn’t belong there!)
You mentioned adding googlemaps to settings.py—but here’s a key point: INSTALLED_APPS is only for Django-specific applications (like your custom app, or libraries like django-rest-framework that integrate with Django’s ecosystem). The googlemaps library is a general Python package, not a Django app, so adding it here won’t help and might cause unnecessary confusion. Just delete it from the INSTALLED_APPS list.
4. Fix your view’s return value (bonus tip!)
While this isn’t related to the ModuleNotFoundError, your current view will throw another error once the library is working: Django views need to return a valid HTTP response object, not just a raw list. Update your index view to return a JsonResponse instead:
from django.shortcuts import render from django.http import JsonResponse import googlemaps gmaps = googlemaps.Client(key='YOUR_ACTUAL_API_KEY') def index(request): if request.method == "GET": geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA') return JsonResponse(geocode_result, safe=False)
5. Reinstall the library if all else fails
Sometimes installations get corrupted. Try uninstalling and reinstalling googlemaps to rule this out:
pip uninstall googlemaps pip install googlemaps
内容的提问来源于stack exchange,提问作者Ben.W

