[英]How do I find out whether a Python module is run on GAE?
I have a module that uses lxml.我有一个使用 lxml 的模块。 Since this cannot be imported on GAE, I'd like to use a suitable substitute by default.由于这不能在 GAE 上导入,我想默认使用合适的替代品。 Something along the lines of:类似于以下内容:
if not ON_GAE:
import lxml
else:
import beautifulsoup
How can I determine that I'm on GAE?如何确定我在 GAE 上? Is there a OS variable of some sorts?是否有某种操作系统变量?
Update: There are certain modules that will not run on GAE (like sockets
).更新:某些模块无法在 GAE 上运行(例如sockets
)。 Rather than having multiple blocks of try
... except ImportError
, I'd like to know from the start which code blocks needs an alternative implementation. except ImportError
,我不想拥有多个try
... 块,而是想从一开始就知道哪些代码块需要替代实现。
You can use this:你可以使用这个:
on_app_engine = os.environ.get('SERVER_SOFTWARE', '').startswith('Google')
Then something like:然后是这样的:
if on_app_engine:
import lxml
else:
import bla
You can simply try and see if an import throws an exception and use the other import only if it's necessary.您可以简单地尝试查看导入是否引发异常,并仅在必要时使用另一个导入。
try:
import lxml
except ImportError:
import beautifulsoup
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.