1

I have recently run into an import error with psutils in the types.py called from google.cloud.pubsub_v1 on a GAE standard project inside Pycharm. I messed around for a day trying to find away around it, but no go. psutil imports in python run from the terminal and the console and from a vanilla python project but not from a GAE project. To double up on this I made a basic test of the two as seen below: Both using the same virtualenv...


Standard Vanilla Python Project:

main.py contains

import psutil

def x():
    print "hello world"

if __name__ == "__main__":
    x()

Google App Engine Project - main.py contains

import webapp2
import psutil

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello world!')

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

The import in the vanilla python project works fine, but the import in the GAE app returns the traceback:

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/chez 1/google-app-engine-projects/untitled/main.py", line 18, in <module>
    import psutil
ImportError: No module named psutil

Has anyone had similar problems and found a fix?

Chez
  • 961
  • 1
  • 9
  • 20

2 Answers2

0

You're likely having the psutil package installed on your local system (thus working in terminal/console), but not vendored into your app (i.e installed in your app's lib dir) as required for standard env apps (the SDK emulates the real GAE, for which whatever is installed into your local python installation means nothing). From Installing a third-party library:

  1. Create a directory to store your third-party libraries, such as lib/.

    mkdir lib
    
  2. Use pip (version 6 or later) with the -t <directory> flag to copy the libraries into the folder you created in the previous step. For example:

    pip install -t lib/ <library_name>
    

So just vendor into your app the psutil package.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • I've tried that and I get the following error: ImportError: No module named psutil._psutil_osx – Chez Oct 20 '17 at 05:36
  • Hm, that package is likely to violate the GAE sandbox restrictions, it *might* have something to do with that: https://cloud.google.com/appengine/docs/standard/python/runtime#Python_The_sandbox – Dan Cornilescu Oct 20 '17 at 05:46
  • Well, if it does why use it in a GAE package? – Chez Oct 20 '17 at 08:51
  • Well, pub/sub wasn't exactly designed for GAE, plus standard and flex GAE are some significantly different beasts. Hm, flex has its own [pub/sub doc](https://cloud.google.com/appengine/docs/flexible/python/writing-and-responding-to-pub-sub-messages), the standard env doesn't. I admit, I'm speculating. – Dan Cornilescu Oct 20 '17 at 15:04
  • In [cloud-pubsub-samples-python](https://github.com/GoogleCloudPlatform/cloud-pubsub-samples-python/tree/master/appengine-push) I see they have only `google-api-python-client` in requirements.txt, they don't specifically import `psutils`. – Dan Cornilescu Oct 20 '17 at 15:30
  • So what did you end up doing finally @Chez, cause i have run into exactly same situation – Anuj Dec 04 '17 at 09:22
  • Anuj, you have a few choices. Use the standard google client api for access to use pub/sub or use some other push notification system. You can also write a method on the Google cloud in say node.js that you can call which in turn calls the pub/sub routine. – Chez Dec 19 '17 at 09:53
0

In my experience this and similar 'No module named ...' errors have been caused by an incorrectly defined python interpreter. See the answers at: ImportError: No module named 'bottle' - PyCharm

James
  • 184
  • 1
  • 5