如何使用Python组织LIB+;谷歌应用程序引擎&x2B;非统组织
我的问题是以下错误:如何使用Python组织LIB+;谷歌应用程序引擎&x2B;非统组织,python,google-app-engine,Python,Google App Engine,我的问题是以下错误: ImportError: No module named oauthlib.oauth2 因此,我似乎无法在gae开发环境中导入 我有以下项目结构 app_root/ app/ - source files of the app libs/ - third party libraries gdata/ - google data library atom/ - requires
ImportError: No module named oauthlib.oauth2
因此,我似乎无法在gae开发环境中导入
我有以下项目结构
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
因此,当我从启动应用程序时,会出现以下错误:
from oauthlib.oauth2 import RequestValidator, WebApplicationServer
ImportError: No module named oauthlib.oauth2
我真的不明白为什么会发生这种情况,在我的IDE中,它没有突出显示导入为红色,所以它可以看到引用是好的
我对python有点陌生,所有这些使用第三方库的方法都让我感到困惑。事实上,关于这一点有很多问题,比如有没有更好的方法来管理gae的第三方库
但让我们把重点放在主要问题上。当引用应该是好的时,为什么它会抛出错误
更新#1
如果我将libs作为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.prefixIIRC,您应该能够通过在路径的正确位置放置一个简单的
\uuuu init\uuuuuu.py
来实现这一点:
app_root/
app/ - source files of the app
libs/ - third party libraries
__init__.py - # Add this
gdata/ - google data library
atom/ - requires by google data
oauthlib/ - [oauthlib][1]
test/ - unit tests
app.yaml - gae config
main.py - main script
现在,您可以执行以下操作,而不是乱搞sys.path
:
from libs.oauthlib.oauth2 import RequestValidator, WebApplicationServer
如果你真的想继续玩你的系统路径,那么我认为你需要另一个
os.path.dirname
,因为你的.py
文件似乎在应用程序中。实际上,您似乎在python源文件所在的目录中查找lib
:
app_dir = os.path.dirname(__file__)
root_dir = os.path.dirname(app_dir)
sys.path.append(os.path.join(root_dir, 'libs'))
请注意main.py(我在其中扩展系统路径)与lib文件夹位于同一级别,因此我不会在app文件夹中查找lib文件夹,因为您的第一个建议我更新了我的问题