简体   繁体   中英

How do I find out whether a Python module is run on GAE?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM