5

I'm trying to use a third-party lib (docutils) on Google App Engine and have a problem with this code (in docutils):

try:
    import pwd
    do stuff
except ImportError:
    do other stuff

I want the import to fail, as it will on the actual GAE server, but the problem is that it doesn't fail on my development box (ubuntu). How to make it fail, given that the import is not in my own code?

jerd
  • 75
  • 6

2 Answers2

12

Even easier than messing with __import__ is just inserting None in the sys.modules dict:

>>> import sys
>>> sys.modules['pwd'] = None
>>> import pwd
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pwd
Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
  • And I realized the hidden features of Python question was missing this one, so I added it there, as well: http://stackoverflow.com/questions/101268/hidden-features-of-python/2259080#2259080 – Thomas Wouters Feb 13 '10 at 21:22
  • 1
    11 1/2 years later and I get something different: `ModuleNotFoundError`... both if there is genuinely no such module and also if `sys.modules` has been set as above. The messages are slightly different in the two cases. For the purpose of testing this works fine though. – mike rodent Oct 23 '21 at 16:50
4

In your testing framework, before you cause docutils to be imported, you can perform this setup task:

import __builtin__
self.savimport = __builtin__.__import__
def myimport(name, *a):
  if name=='pwd': raise ImportError
  return self.savimport(name, *a)
__builtin__.__import__ = myimport

and of course in teardown put things back to normal:

__builtin__.__import__ = self.savimport

Explanation: all import operations go through __builtin__.__import__, and you can reassign that name to have such operations use your own code (alternatives such as import hooks are better for such purposes as performing import from non-filesystem sources, but for purposes such as yours, overriding __builtin__.__import__, as you see above, affords truly simple code).

u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Thanks for the quick response, that looks like it should do the trick. – jerd Feb 13 '10 at 16:57
  • Unfortunately this didn't work - perhaps it's an AppEngine thing? In any case, Thomas Wouters' method did what I wanted. All the best. – jerd Feb 13 '10 at 21:40
  • It wouldn't work in App Engine -- but then in App Engine `import pwd` just fails, so you don't need this to work in App Engine itself, but rather _outside_ of it when you're simulating it for testing. Still, Thomas's answer is simpler and shorter, which makes it more fit for purpose, so I agree w/your choice (and I'm going to +1 his A -- remember to come back and do the same when your reputation grows enough to allow it!-). – Alex Martelli Feb 13 '10 at 23:00
  • `import pwd` fails in App Engine production, but not in the App Engine development server - I think you were mislead by the use of the word 'fail' in my post, I'm not in any particular testing framework here, this is code which I *know* will fail on App Engine but refuses to fail when running within `dev_appserver.py`. I'm speculating 'an App Engine thing' with regard to `__builtin__` in part because I'm also having problems with the `gettext` underscore - `gettext` also manipulates the __builtin__ dict, but code which works outside of dev_appserver.py doesn't work inside (no attribute '_'). – jerd Feb 14 '10 at 16:12