0

I am trying to use Boto3 in order to manage some EC2 instances from my GAE app, but importing Boto3 results in the following error:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
  File "/Users/squad/Desktop/Squad/Squad/squad.py", line 6, in <module>
from boto3 import Session
  File "/Users/squad/Desktop/Squad/Squad/lib/boto3/__init__.py", line 16, in <module>
from boto3.session import Session
  File "/Users/squad/Desktop/Squad/Squad/lib/boto3/session.py", line 17, in <module>
import botocore.session
  File "/Users/squad/Desktop/Squad/Squad/lib/botocore/session.py", line 32, in <module>
from botocore.loaders import create_loader
  File "/Users/squad/Desktop/Squad/Squad/lib/botocore/loaders.py", line 188, in <module>
class Loader(object):
  File "/Users/squad/Desktop/Squad/Squad/lib/botocore/loaders.py", line 201, in Loader
CUSTOMER_DATA_PATH = os.path.join(os.path.expanduser('~'),
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", line 261, in expanduser
import pwd
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 963, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named pwd

I am able to use Boto just fine, so this in no way is a pressing need, but I would prefer to use Boto3 if possible.

I am using Python2.7, if that is of any help.

Thanks for the help.

  • This is duplicate of: http://stackoverflow.com/questions/18819604/importerror-no-module-named-pwd-but-it-exists – Destrif May 18 '16 at 07:42
  • Does this also manifest itself when deployed (not just in a developer server)? If yes, my guess is that this import is disallowed by the sandbox, in which case flexible VMs (https://cloud.google.com/appengine/docs/flexible/python/quickstart) may be a workaround (if it's not possible to load boto in a way that does not perform these operations). – Michael Aaron Safyan May 18 '16 at 07:42

1 Answers1

0

You will have to fake the pwd module.

Create a file named fake_pwd.py with the necessary shims:

class struct_passwd_dummy(object):

    def __init__(self, uid):
        self.pw_name = "user"
        self.pw_passwd = "x"
        self.pw_uid = uid
        self.pw_gid = uid
        self.pw_gecos = "user"
        self.pw_dir = "/home/user"
        self.pw_shell = "/bin/sh"


def getpwuid(uid):
    return struct_passwd_dummy(uid)

Then, in appengine_config.py, try this hack:

try:
    import boto3
except ImportError:
    import sys
    import fake_pwd
    sys.modules["pwd"] = fake_pwd
    import boto3