unittests require google.appengine.ext - python

I have Google App Engine Python SDK installed and working with the GUI. However, now I am locally trying to test my app using unittest in a virtualenv. Everything is fine until I try to access google.appengine.ext.testbed. How do I access the SDK in this context? My error is included below.
Traceback (most recent call last):
File "tests.py", line 4, in <module>
from google.appengine.ext import testbed
ImportError: No module named google.appengine.ext
update: In further research, I've found that GAE "doesn't play well" with virtualenv. Subsequently, I installed all of my dependencies globally. The issue remains unchanged, so I think we can rule out virtualenv as the culprit.

Related

Won't import local file as module

I'm setting up a Flask application on Digitalocean and have Python 3.7 installed and the latest version of Flask. When running the app inside a virtualenv and trying to run the application using python3.7 application.py I get the following error message:
Traceback (most recent call last):
File "application.py", line 11, in <module>
from config import *
ModuleNotFoundError: No module named 'config'
What puzzles me is that config.py is located in the same folder as application.py, and not in a subfolder. I have duplicated the setup on my local machine, also running Python 3.7 and inside a virtualenv, and the importing (and the app) works flawlessly.
I've tried importing "config.py" instead of just "config" but didn't make a difference. I also tried specifying exactly what it should import (instead of using '*') but that didn't make a difference either.
Your thoughts on why it can't find config?
What seems to have been the solution to my problem above was to run the Python shell and add the path to the directory in which config.py is located (even though it's in the same folder as application.py...) by using the following command:
sys.path.append("/path/to/config/")

import cloudstorage, ImportError: No module named google.appengine.api

I would like to use Google Cloud Storage Client Library Functions.
For that I have to import the cloudstorag. To get the cloudstorage I download Google Cloud Storage client library.
I try to import cloudstorage using python -c "import cloudstorage". I get the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "cloudstorage/__init__.py", line 20, in <module>
from .api_utils import RetryParams
File "cloudstorage/api_utils.py", line 45, in <module>
from google.appengine.api import app_identity
ImportError: No module named google.appengine.api
Am I missing something?
When you execute python -c "import cloudstorage" you're attempting to run a standalone application. But the GCS library you're trying to use is for a (standard environment) GAE application, which cannot be executed as a standalone app, it needs to run in a GAE sandbox (locally that's dev_appserver.py). See GAE: AssertionError: No api proxy found for service "datastore_v3".
And the library needs to be installed inside your GAE app, see Copying a third-party library.
If you're not developing a standard env GAE app and indeed you want to write a standalone one, you're not looking at the right documentation. You need to use a different library than the GAE-specific one(s). See Cloud Storage Client Libraries
You can add this lines, which will add the path of the sdk tools:
import pkgutil
import google
google.__path__ = pkgutil.extend_path(google.__path__, google.__name__)
For unittesting, it could be useful to run in standalone mode.
Looks like gcloud is not installed on your system.
pip install --upgrade gcloud
pip install --upgrade google-api-python-client

Import error on first-party library with dev_appserver.py

On Ubuntu 16.04, am suddenly getting import errors from the local GAE development server.
The local dev server starts up, including the admin interface, but app no longer loads.
Native python imports of the same library on same machine (in this case "from google.cloud import datastore") work fine.
The GAE standard app does run when deployed, but development just got a little challenging.
google-cloud is version 0.27.0
gcloud components is 172.0.1
python is Anaconda 2.7.13
GAE is standard
I have confirmed to the best of my middling abilities that $PATH is correct for all named libraries.
I have removed and re-added all the named libraries to no effect.
cachetools(2.0.1) it should probably be noted, is installed as a dependency of the google cloud libraries, so I don't think this is addressable through requirements.txt or "libraries" in app.yaml.
I did recently go through a cycle of removing and adding libraries to fix a problem with apache_beam 2.0.1, so I may have jacked up something else, but am not sure where to look.
Suggestions deeply appreciated. Full traceback (from admin, same as from app):
Traceback (most recent call last):
File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/request_handler.py", line 232, in handle_interactive_request
exec(compiled_code, self._command_globals)
File "<string>", line 1, in <module>
File "/home/brian/anaconda3/lib/python2.7/site-packages/google/cloud/datastore/__init__.py", line 61, in <module>
from google.cloud.datastore.client import Client
File "/home/brian/anaconda3/lib/python2.7/site-packages/google/cloud/datastore/client.py", line 23, in <module>
from google.cloud.client import ClientWithProject
File "/home/brian/anaconda3/lib/python2.7/site-packages/google/cloud/client.py", line 27, in <module>
from google.oauth2 import service_account
File "/home/brian/anaconda3/lib/python2.7/site-packages/google/oauth2/service_account.py", line 79, in <module>
from google.auth import jwt
File "/home/brian/anaconda3/lib/python2.7/site-packages/google/auth/jwt.py", line 49, in <module>
import cachetools
ImportError: No module named cachetools
The stacktrace indicates you're running the libraries from the local system installation (the site-packages dir), not from your app.
For standard env GAE apps you need to install the dependencies inside your app and they will be uploaded to GAE together with your app code.
More specifically you need to use the -t <your_app_lib_dir> option for the pip installation. From Installing a third-party library:
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>
I addressed my problem through the requirements.txt file in app root directory.
I had: google-cloud==0.22.0
and changed it to: google-cloud==0.27.0
which fixed it.

Python Import Error when working with GoogleAppEngine

I have Google AppEngine SDK installed(at /usr/local/google-appengine), and the toy App can be launched and run with GoogleAppEngine Launcher with no issue.
However, I have some standalone scripts (testpbuf.py) in the app folder that I want to run with
$python testpbuf.py
then I got Python Import errors:
Traceback (most recent call last):
File "testpbuf.py", line 3, in <module>
from google.appengine.api import files
ImportError: No module named appengine.api
The script is trying to import AppEngine API and protorpc modules..
What's going one here? I have the SDK included in my PATH and I have no problem invoking from terminal. Any insights? Thanks a lot!
You need to get all the libraries inside the SDK added to your path. This can be done as follows:
import dev_appserver
dev_appserver.fix_sys_path()
Running code outside of the SDK web-server for things other than tests isn't likely to be that useful to you though ... when it runs on appengine, it has to be via WSGI.

Is the routes Dispatcher broken in CherryPy for Mac?

Is CherryPy broken? I just set it up and tried to use the routes dispatcher but it has an import error, my code is as follows:
import cherrypy
mapper = cherrypy.dispatch.RoutesDispatcher()
The error is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jwesonga/environments/cherrypy/lib/python2.6/site-packages/CherryPy-3.2.2-py2.6.egg/cherrypy/_cpdispatch.py", line 463, in __init__
import routes
ImportError: No module named routes
I'm on a Mac and I tried both 3.2.2 and 3.0 using virtualenv for the latter.
I have successfully used CherryPy with the routes dispatcher under OS X.
The error you've shown is:
ImportError: No module named routes
This is pretty clear -- Python can't find the routes modules. Have you installed it? This is not part of CherryPy, it's a separate module that you will need to install. If you're using MacPorts, you should be able to:
port install py-routes
(Or py25-routes or py26-routes depending on which Python you're using). If you're using virtualenv, you can simply run:
easy_install routes

Categories