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"

View this page 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 https://github.com/nltk/nltk/blob/develop/nltk/__init__.py

Leave a Reply