Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting whether a module is imported inside the App Engine environment

What I want to do is patch an existing Python module that uses urllib2 to run on App Engine, but I don't want to break it so it can be used elsewhere. So I'm looking for a quick solution to test if the module is imported in the App Engine environment or not. Catching ImportError on urllib2 might not be the best solution.

like image 498
Vasil Avatar asked Dec 22 '22 13:12

Vasil


1 Answers

You could simply use sys.modules to test if a module has been imported (I'm using unicodedata as an example):

>>> import sys
>>> 'unicodedata' in sys.modules
False
>>> import unicodedata
>>> 'unicodedata' in sys.modules
True
like image 81
chryss Avatar answered May 18 '23 23:05

chryss