如何从 Python Google App Engine 中的模块访问供应商库?

如何从 Python Google App Engine 中的模块访问供应商库?

问题描述:

当我尝试访问应用程序根目录中的 lib 目录中的库时,出现导入错误.

I am getting an ImportError when I try to access a library inside the lib directory in my application root.

我在文档中执行了必要的步骤,并且只使用一个模块,但是一旦我移动到这个包含多个模块的目录结构,它就不再工作了:

I did the necessary steps in the documentation and it worked with only one module but once I moved to this directory structure with multiple modules it no longer works:

<app root>
  |
  +-- appengine_config.py
  |
  +-- lib
  |
  +-- modules     
      +-- module1
      |
      +-- module2

在我的 appengine_config.py 中,我添加了以下行:vendor.add(os.path.join(os.path.dirname(__file__), 'lib'))

In my appengine_config.py I add the line: vendor.add(os.path.join(os.path.dirname(__file__), 'lib'))

对这里可能发生的事情有什么想法吗?

Any thoughts on what might be going on here?

这个目录结构似乎对我有用:

This directory structure that seemed to work for me:

<app root>
  |
  +-- lib
  |     
  +-- module1
  |     +-- appengine_config.py
  |     |
  |     +-- app.yaml
  |     |
  |     +-- lib <symlink to app root lib>
  +-- module2 (same as module 1)

我认为 appengine_config.py 是在主应用程序级别而不是模块级别定义的东西.在每个模块中复制完全相同的文件和符号链接是否正确.

I thought that appengine_config.py was something to be defined at the main application level and not the module level. Is it correct to duplicate the same exact file and sym linking in each module.

每个模块目录都作为一个独立的应用程序上传,并且它需要该模块目录中的所有文件(即模块的父级没有任何内容)目录已上传/在模块中可见).模块目录是包含模块的 .yaml 文件的目录,因此在您的情况下,它们将是 modules/module1modules/module2目录.

Each module directory is uploaded as a standalone application and it needs all its files inside that module directory (i.e. nothing from the module's parent directory is uploaded/visible in the module). The module directory is the one containing the module's .yaml file, so in your case they'll be the modules/module1 and modules/module2 dirs.

因此将 lib 目录和 appengine_config.py 从应用程序的目录符号链接到模块的目录中.此外,对于我的应用程序,正如 Tim 提到的,有效的 vendoring 行确实只是 vendor.add('lib').复制它们也一样有效,但符号链接更好,因为您只需要在版本控制系统中维护一份副本,本着 DRY 精神.appcfg.py 脚本知道跟随符号链接并上传符号链接指向的实际文件.

So symlink the lib dir and the appengine_config.py from the app's dir into the modules' dirs. In addition, for my app, as Tim mentions, the vendoring line that works is indeed just vendor.add('lib'). Copying them works just as well, but symlinking is better as you only need to maintain one copy in your version control system, in the DRY spirit. The appcfg.py script knows to follow the symlinks and upload the actuall files the symlinks point to.

如果您不需要all而只需要一些模块中的库,您可以在该模块目录中创建一个lib目录并且仅将应用程序的 lib 目录的必要子目录符号链接到相应模块的 lib 目录(以免上传不必要的文件).

If you don't need all but only some of the libs in a module you can create a lib dir in that module dir and only symlink the necessary subdirs of the app's lib dir into the respective module's lib dir (to not upload unnecessary files).

可能还需要符号链接到其他应用程序级配置文件的某些模块目录中,以使某些支持脚本在开发服务器上正常运行,请参阅此答案:https://stackoverflow.com/a/34111170/4495081.请注意,更新应用程序级别的配置可能不会像单模块应用程序教程中提到的那样简单地通过上传模块的代码来实现,它们需要使用 appcfg.py的相应参数进行显式请求>,如该答案的备忘单中所述.

You might need to also symlink into some of the module dirs other app-level config files to keep some of the supporting scripts happy on the development server, see this answer: https://stackoverflow.com/a/34111170/4495081. Note that updating the app-level configs may not happen simply by uploading the modules' code as mentioned in the single-module app tutorials, they'll need to be requested explicitly with the corresponding arguments of appcfg.py, as mentioned in the cheat sheet in that answer.