如何解决Google App Engine dev_appserver.py中的Python子模块导入错误?
six.moves Error in App Engine's appengine_config.py Hey there, let's break down what's happening and fix this issue with six.moves in your App Engine project.
First, let's recap your setup: You added code in appengine_config.py to import six, print its version (1.11.0) and the six.moves module (which shows as a built-in module), but then hit a runtime error. Here's what's likely going on and how to fix it:
Possible Causes
The error is probably due to App Engine's sandbox environment handling the built-in six module differently—even though the version looks valid, the six.moves module might be missing some submodules you're trying to use, or the built-in version conflicts with your project's needs.
Fixes to Try
1. Vendor Your Own Copy of six
App Engine provides some built-in libraries, but sometimes their versions or configurations don't match project requirements. The best fix here is to bundle your own copy of six with the project:
- Create a
requirements.txtfile in your project root with this line:six==1.11.0 - Install the dependency into a
libdirectory (this is App Engine's standard way to bundle third-party libs):pip install -t lib -r requirements.txt - Update your
appengine_config.pyto load the vendored library first (before importingsix):from google.appengine.ext import vendor # Add the lib directory to the Python path vendor.add('lib') # Now import six import six print(six.__version__) print(six.moves) # Your other six.moves usage here
This ensures your project uses the exact six version you installed, not the built-in one that's causing issues.
2. Check Specific six.moves Submodule Usage
If the error happens when you try to import a specific submodule (like six.moves.urllib or six.moves.http_client), double-check:
- That the submodule exists in
six1.11.0 (you can verify this locally by runningpython -c "import six.moves.xxx"on your machine) - That App Engine's sandbox allows access to that underlying module. For example, some low-level network modules might have restrictions, so you might need to use App Engine's native alternatives (like
google.appengine.api.urlfetchinstead ofurllib).
3. Get the Full Traceback
The error snippet you shared is truncated—if you can get the full traceback, it will show exactly which line is failing (e.g., an ImportError for a specific submodule). This will make troubleshooting even more targeted.
内容的提问来源于stack exchange,提问作者EvilTosha

