Sebastian
Sebastian

Reputation: 1095

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.

Upvotes: 2

Views: 183

Answers (2)

Tugrul Ates
Tugrul Ates

Reputation: 9687

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

Upvotes: 2

Andz
Andz

Reputation: 2258

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

Upvotes: 4

Related Questions