在virtualenv中使用dev_appserver.py时导入错误

时间:2017-02-21 19:08:00

标签: python google-app-engine virtualenv

当我在使用Python 2.7.12的virtualenv中运行dev_appserver.py .(在包含app.py的目录中)时,我收到此错误并输出:

(.venv)$ dev_appserver.py .                                                              
INFO     2017-02-21 18:54:47,250 devappserver2.py:764] Skipping SDK update check.
INFO     2017-02-21 18:54:47,273 api_server.py:268] Starting API server at: http://localhost:35473
INFO     2017-02-21 18:54:47,276 dispatcher.py:199] Starting module "default" running at: http://localhost:8080
INFO     2017-02-21 18:54:47,276 admin_server.py:116] Starting admin server at: http://localhost:8000
Traceback (most recent call last):
  File "/opt/gcloud/google-cloud-sdk/platform/google_appengine/_python_runtime.py", line 101, in <module>
_run_file(__file__, globals())
  File "/opt/gcloud/google-cloud-sdk/platform/google_appengine/_python_runtime.py", line 97, in _run_file
execfile(_PATHS.script_file(script_name), globals_)
  File "/opt/gcloud/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime.py", line 185, in <module>
main()
  File "/opt/gcloud/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime.py", line 165, in main
sandbox.enable_sandbox(config)
  File "/opt/gcloud/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 198, in enable_sandbox
__import__('%s.threading' % dist27.__name__)
  File "/opt/gcloud/google-cloud-sdk/platform/google_appengine/google/appengine/dist27/threading.py", line 11, in <module>
import warnings
  File "/opt/gcloud/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 1001, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named warnings

当我不在virtualenv时工作正常。正如我所理解的那样,warnings模块是python标准库的一部分,所以不知道该怎么做。正在运行pip install warnings无济于事。

如何让dev_appserver.py在virtualenv中运行?

1 个答案:

答案 0 :(得分:4)

我没有以适当的方式使用dev_appserver.py和virtualenv。 Virtualenv不能在这里使用。找到有关使用第三方库的相关文档here

简而言之,要将第三方库包含在dev_appserver.py中:

指示pip将库存储在具有-t标志的文件夹中:

$ pip install -t lib/ <library name>

在与appengine_config.py相同的文件夹中创建名为app的文件,并包含以下代码:

from google.appengine.ext import vendor

# Add any libraries install in the "lib" folder.
vendor.add('lib')

现在像往常一样运行dev_appserver.py:$ dev_appserver.py app.yaml

请记住,您只能使用使用纯Python代码的Python库(例如,不能使用bcrypt库。)。