Windows 10下运行Python 2.7项目遇ImportError: No module named appengine.api
Hey there, I’ve dealt with this exact frustrating error before when working with Python 2.7 and Google App Engine on Windows 10. Let’s go through the most reliable fixes step by step:
1. Ensure the App Engine SDK path is in your Python environment
The most common culprit is that Python can’t locate the App Engine libraries. You have two straightforward ways to fix this:
Option A: Manually add the path in your code
At the very top of your Python script, add these lines to explicitly point Python to the App Engine SDK directory. Replace the path with the actual location of your Google Cloud SDK’s platform/google_appengine folder (it’s typically in C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine):
import sys sys.path.insert(0, 'C:/path/to/google-cloud-sdk/platform/google_appengine')
Option B: Set the PYTHONPATH environment variable
This makes the path permanent so you don’t have to add it to every script:
- Open Windows Settings → System → About → Advanced system settings → Environment Variables
- Under "System Variables", click "New"
- Set Variable name to
PYTHONPATH - Set Variable value to the full path of the
google_appenginefolder mentioned above - Restart your command prompt or IDE for the changes to take effect
2. Verify you’re using Python 2.7
Even if you installed Python 2.7, Windows might default to a newer Python 3.x version without you noticing:
- Open a command prompt and run
python --version - If it shows Python 3.x, try running your script with
python2.7 your_script.pyinstead - Alternatively, adjust your
PATHenvironment variable to move the Python 2.7 directory above any Python 3.x entries
3. Reinstall the App Engine Python component
Sometimes the initial Cloud SDK install misses critical App Engine libraries. Open the Google Cloud SDK Shell and run:
gcloud components install app-engine-python
This ensures all necessary App Engine modules are installed and up to date.
4. Use the App Engine dev server to run your project
Instead of running your script directly with Python, use the official dev_appserver.py tool—it automatically configures the correct Python path for App Engine:
- Navigate to your project directory in the command prompt
- Run:
dev_appserver.py app.yaml
(Note: This requires an app.yaml configuration file in your project, which is standard for App Engine apps.)
A quick heads-up: Python 2.7 is no longer supported by the Python Software Foundation, so if you’re starting a new project, consider migrating to Python 3 with the newer App Engine runtime. But for existing Python 2.7 projects, the fixes above should get you up and running.
内容的提问来源于stack exchange,提问作者user2547656

