【发布时间】:2013-12-11 00:28:23
【问题描述】:
我的问题是以下错误:
ImportError: No module named oauthlib.oauth2
看来我无法在 gae dev environemnt 中导入 oauthlib
我有以下项目结构
app_root/
app/ - source files of the app
libs/ - third party libraries
gdata/ - google data library
atom/ - requires by google data
oauthlib/ - [oauthlib][1]
test/ - unit tests
app.yaml - gae config
main.py - main script
这里是app.yaml的来源
application: app
version: v1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
在 main.py 中,我有以下代码可以在“libs”文件夹中获取:
sys.path.append(os.path.join(os.path.dirname(__file__), 'libs'))
最后我的模块中有以下导入:
from oauthlib.oauth2 import RequestValidator, WebApplicationServer
结果当我从pycharm 启动应用程序时,我收到此错误:
from oauthlib.oauth2 import RequestValidator, WebApplicationServer
ImportError: No module named oauthlib.oauth2
我真的不明白为什么会这样,在我的 IDE 中它没有将导入突出显示为红色,所以它可以看到引用是好的。
我对 python 有点陌生,所有这些使用第三方库的方法真的让我很困惑。实际上有很多关于这个的问题,比如有没有更好的方法来管理 gae 的第三方库。
但让我们专注于主要问题。为什么引用应该没问题时会抛出 ImportError?
更新 #1 如果我将库作为 python 包,那么 init.py 它破坏了库代码中的导入,即文件 oauthlib/oauth2/rfc6749/clients/base.py 包含:
from oauthlib.oauth2.rfc6749 import tokens
from oauthlib.oauth2.rfc6749.parameters import prepare_token_request
from oauthlib.oauth2.rfc6749.errors import TokenExpiredError
from oauthlib.oauth2.rfc6749.errors import InsecureTransportError
from oauthlib.oauth2.rfc6749.utils import is_secure_transport
但当前模块路径包含 libs.prefix
【问题讨论】: