如何在PythonAnywhere的web2py中安装并导入googlemaps API模块
Hey there! I totally get the confusion when moving from local dev to a hosted environment like PythonAnywhere—let’s get this sorted step by step.
Step 1: Access the PythonAnywhere Bash Console
First, log into your PythonAnywhere account, then click on the Consoles tab in the top navigation. From there, launch a Bash console—this is your online terminal, just like the one you use locally.
Step 2: Confirm Your web2py App’s Python Version
Web2py on PythonAnywhere might be using a specific Python version (e.g., 3.8, 3.9). To check which one your app is running:
- Go to the Web tab, find your web2py app, and look for the "Python version" dropdown under the "Code" section.
- Alternatively, run this in the Bash console to verify the default version (if you haven’t customized it):
Or for Python 3 specifically:python --versionpython3 --version
Step 3: Install the Google Maps Module with pip
Since you can’t install packages globally on PythonAnywhere (you don’t have admin rights), use the --user flag to install the module to your user directory, which web2py can access.
If your app uses Python 3, run:
pip3 install --user googlemaps
For Python 2 (though it’s deprecated, just in case):
pip install --user googlemaps
Step 4: Test the Import in web2py
Once installed, you can import the module directly in your web2py controllers or models, just like you do locally:
import googlemaps from datetime import datetime # Initialize the client with your API key gmaps = googlemaps.Client(key='YOUR_GOOGLE_MAPS_API_KEY')
Step 5: Reload Your web2py App
If you get an import error after installing, don’t panic! Head back to the Web tab in PythonAnywhere, find your web2py app, and click the Reload button. This ensures the app picks up the newly installed module.
A quick note: Make sure you’ve enabled the Google Maps API in your Google Cloud Console and have a valid API key—this is separate from installing the Python module, but easy to forget!
内容的提问来源于stack exchange,提问作者Uchoud1

