python - 仅在某些文件中出现“导入错误,没有名为 httplib2 的模块”

python - " Import error no module named httplib2" in only some files

当我使用终端在 quickStart.py 和 运行 中导入 httplib2 时它起作用了。

现在我在另一个文件 main.py(Google App Engine Web 应用程序 python 文件)中导入 quickStart 并尝试通过本地主机加载页面,它显示 "Import error no module named httplib2" 同时两者文件在同一目录中。 它显示以下错误:-

ERROR    2015-10-13 12:41:47,128 wsgi.py:263]

Traceback (most recent call last):
  File "C:\Program Files (x86)\google\google_appengine\google\appengine\runtime\wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "C:\Program Files (x86)\google\google_appengine\google\appengine\runtime\wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "C:\Program Files (x86)\google\google_appengine\google\appengine\runtime\wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "G:\dtuwiki\dtuwiki2\main.py", line 7, in <module>
    import quickStart
  File "G:\dtuwiki\dtuwiki2\quickStart.py", line 2, in <module>
    import httplib2
ImportError: No module named httplib2
INFO     2015-10-13 18:11:47,398 module.py:809] default: "GET / HTTP/1.1" 500 -

main.py

  import webapp2
    import jinja2
    import os 
    import cgi
    import quickStart
    template_dir = os.path.join(os.path.dirname(__file__), 'templates')


    root_dir = os.path.dirname(__file__)
    jinja_env = 

jinja2.Environment(loader=jinja2.FileSystemLoader([template_dir,root_dir]),autoescape=True)

    def escapeHTML(string):
        return cgi.escape(string , quote="True")


class Handler(webapp2.RequestHandler):
    def write(self,*a,**kw):
        #self.response.write(form %{"error":error})
        self.response.out.write(*a,**kw)
    def render_str(self,template,**params):
        t = jinja_env.get_template(template)
        return t.render(params)     
    def render(self , template ,**kw):
        self.write(self.render_str(template,**kw))

quickStart.py

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

import datetime

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Python Quickstart'


def get_credentials():

    home_dir = os.path.expanduser('~')  
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'calendar-python-quickstart.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatability with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

我也试过以下--

$ python -m pip install httplib2
Requirement already satisfied (use --upgrade to upgrade): httplib2 in /usr/local/lib/python2.7/site-packages
Cleaning up...

C:\WINDOWS\system32>python -m pip -V
pip 7.1.2 from C:\Python27\lib\site-packages (python 2.7)

C:\WINDOWS\system32>python -m pip list
google-api-python-client (1.4.2)
httplib2 (0.9.2)
Jinja2 (2.8)
oauth2client (1.5.1)
pip (7.1.2)
uritemplate (0.6)
virtualenv (13.1.2)

Google App Engine 要求应用程序源代码树中包含任何第 3 方模块,以便将其部署到 App Engine。这意味着 site-packages 中的项目不会导入到开发 SDK 下的应用程序 运行 中,您将看到类似于您遇到的错误。

Here are the docs 关于如何包含像 httplib2.

这样的库

简而言之,您需要 pip install -t some_dir <libname> 然后将 some_dir 添加到 appengine_config.py

中的应用程序路径