Python simulate an imported module

Content

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"

Posted . Did this help you? Please let me know with a comment. Thank you

Comments (RSS)

  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

    https://github.com/nltk/nltk/blob/develop/nltk/__init__.py