简体   繁体   English

如何确定 Python 模块是否在 GAE 上运行?

[英]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.

相关问题 如何确定python脚本是作为模块导入还是作为脚本运行? - how do I determine whether a python script is imported as module or run as script? python如何找出是否启用了超线程 - python how to find out whether hyperthreading is enabled 如何找出python模块所属的软件包? - How can I find out what package that a python module belongs to? 如何确定我是否在Linux中安装了Python模块? - How to find out if I have installed a Python module in Linux? 如何使用GAE收听Google表格,然后运行一组python代码? - How do I use GAE to listen to Google sheets and then run a set of python code? 如何确定当前测试是否是最后一次运行? - How do I find out if the current test is the last to be run? 在使用Jena(java)或RDFLib(python)时,我应该如何将本体打开为“turtle”或“xml”? - While using Jena (java) or RDFLib (python), how should I find out whether to open the ontology as 'turtle' or 'xml'? Python BZ2模块顺序解压缩器:如何确定完整文件已成功解压缩的时间? - Python BZ2 Module Sequential decompressor: How do I find out when the complete file has been successfully decompressed? GAE:如何运行dev_appserver.py? - GAE: How do I run dev_appserver.py? 如何测试是否在Python中导入了模块以进行游戏的测试驱动开发? - How do I test whether a module is imported in Python for Test-Driven Development of a game?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM