问题描述:

I have a module that uses lxml. Since this cannot be imported on GAE, I'd like to use a suitable substitute by default. Something along the lines of:

if not ON_GAE:

import lxml

else:

import beautifulsoup

How can I determine that I'm on GAE? Is there a OS variable of some sorts?

Update: There are certain modules that will not run on GAE (like 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.

网友答案:

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
相关阅读:
Top