Python simulate an imported module

Fake a module import by adding a fake module to sys.modules.

import sys
class FakeModule(object):
    PIPE = None
    Popen = None
sys.modules['subprocess'] = FakeModule

Now the following python import from nltk will not fail on App Engine:

from subprocess import PIPE, Popen

Fixes: "ImportError: cannot import name PIPE"

This post is available on GitHub.
Posted .

1 comment

  1. anonymous

    As used in nltk:

    # Override missing methods on environments where it cannot be used like GAE.

    import subprocess

    if not hasattr(subprocess, 'PIPE'):

        def _fake_PIPE(*args, **kwargs):

            raise NotImplementedError('subprocess.PIPE is not supported.')

        subprocess.PIPE = _fake_PIPE

    if not hasattr(subprocess, 'Popen'):

        def _fake_Popen(*args, **kwargs):

            raise NotImplementedError('subprocess.Popen is not supported.')

        subprocess.Popen = _fake_Popen

Leave a Reply