Buy DarkLaunch.com

Dark Launch

Python simulate an imported module

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

Python
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:

Python
from subprocess import PIPE, Popen

Fixes: "ImportError: cannot import name PIPE"

Comments


  1. 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