I'm trying to code a program to check files in Google Cloud Storage using Python.
Now I'm read the google doc -> https://developers.google.com/api-client-library/python/start/installation.
At the last step - "App Engine", it asks user to do following:
->> cd myproject
->> unzip google-api-python-client-gae-1.1.zip
So what's the next steps? I'm still having problem to run the Google sample python code about GCS, which throw error:
Traceback (most recent call last):
File "gcs_test.py", line 22, in <module>
from google.appengine.ext import webapp
ImportError: No module named google.appengine.ext
I tried to add below code but still got same error:
from sys import path
path.append(r"C:\Py_dev\gcs_test\google-api-python-client-gae-1.2.zip")
path.append(r"C:\Py_dev\gcs_test\C:\Py_dev\gcs_test\apiclient")
Thanks for all kind help!
google.appengine is the library used by App Engine apps, not by the api client library, the library you are looking for is apiclient
If you wish to run a normal python script instead of an App Engine app, you should remove references to App Engine specific libraries.
If you are running an app engine app, make sure you are using dev_appserver.py or the actual app engine servers, not just running the script using python
Related
I'm new to flask framework and I want to write a simple flask app that uses another python module (librosa package). I have successfully installed librosa in the same virtual environment that I have installed flask and I can easily import it in the python interpreter. Here is the python script.
# app.py
from flask import Flask
import librosa
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello'
if __name__ == '__main__':
app.run()
The problem is that when I want to run the flask app using the flask run command (after setting export FLASK_APP=app.py) I get the ModuleNotFoundError error as follows:
Error: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "/home/masoud/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/masoud/projects/my_projects/seperator_rewriten/app.py", line 2, in <module>
import librosa
ModuleNotFoundError: No module named 'librosa'
The strange thing is that there is no import error when I run the flask server using python app.py command.
if you are using an IDE such as pycharm, then you may need to install it from the terminal in the IDE itself, not cmd
This may be the solution to your problem:
python -m flask run
The reason is that flask run may use the python executable somewhere else, not the virtual environment you created for the project.
You can add these lines of code in your script:
import sys
import flask
print(sys.executable)
print(flask.__version__)
And then you can check the python executable path using python app.py and flask run.
Using PyCharm Pro 2019.1.1 on MacOS, writing my app in Python 3 w/Flask, Jinja2, etc. A version of my app is already deployed using App Engine Flexible, and I want the next version to use Datastore for my production environment variables. Having problems trying to follow this post, which requires the following import:
from google.appengine.ext import ndb
For the life of me, I cannot seem to get this import working. I'm sure it will work once my app is deployed (famous last words?), but I'd love to get it working locally too.
Initially tried pip install google-appengine and pip install appengine-sdk, only to discover neither could be installed on my venv.
Google Cloud SDK v241.0.0 is installed at /Users/<me>/google-cloud-sdk, and gcloud commands work in the terminal. gcloud components list shows the app-engine-python component is installed, which is probably irrelevant since I'm using a venv in PyCharm.
PyCharm's documentation instructed me to select the App Engine SDK directory under Languages & Frameworks > Google App Engine.
Selecting /Users/<me>/google-cloud-sdk yields this error:
Traceback (most recent call last):
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/pydev/pydevconsole.py", line 5, in <module>
from _pydev_comm.rpc import make_rpc_client, start_rpc_server, start_rpc_server_and_make_client
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/pydev/_pydev_comm/rpc.py", line 4, in <module>
from _pydev_comm.server import TSingleThreadedServer
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/pydev/_pydev_comm/server.py", line 4, in <module>
from _shaded_thriftpy.server import TServer
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/third_party/thriftpy/_shaded_thriftpy/server.py", line 5, in <module>
import logging
File "/Users/<me>/google-cloud-sdk/lib/surface/logging/__init__.py", line 22, in <module>
from googlecloudsdk.calliope import base
ModuleNotFoundError: No module named 'googlecloudsdk'
Selecting /Users/<me>/google-cloud-sdk/platform/google_appengine seems to get me a bit further, but still yields an error:
Traceback (most recent call last):
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/pydev/pydevconsole.py", line 5, in <module>
from _pydev_comm.rpc import make_rpc_client, start_rpc_server, start_rpc_server_and_make_client
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/pydev/_pydev_comm/rpc.py", line 1, in <module>
import socket
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 53, in <module>
from enum import IntEnum, IntFlag
ImportError: cannot import name 'IntFlag' from 'enum' (/Users/<me>/google-cloud-sdk/platform/google_appengine/lib/grpcio-1.9.1/enum/__init__.py)
As the console notes, there is no IntFlag object in 'enum'. These errors are immediately shown when I launch Python Console; I don't even get a cursor. Where am I going wrong?
The post you referenced applies to the 1st generation standard environment - Python 2.7 - that's the only environment in which the ndb library is supported:
The Python NDB Client Library Overview
For the other environments you need to use the generic datastore client library. References:
Using Cloud Datastore - for the 2nd generation standard environment
Using Cloud Datastore - for the flexible environment
Potentially of interest: How to tell if a Google App Engine documentation page applies to the standard or the flexible environment
I am looking to see how others are testing their GAE apps. I've come across a few unit testing frameworks for python (green, pytest, nose, etc).
I then found the tutorial on the GAE site: https://developers.google.com/appengine/docs/python/tools/localunittesting and have been following that.
However, after doing what they say I get the following error:
Traceback (most recent call last):
File "tests/test.py", line 2, in <module>
from google.appengine.ext import db
ImportError: No module named appengine.ext
I am just running
python testrunner.py main/tests usr/local/google_appengine
from the terminal.
Any other ideas?
I have appengine in my pythonpath:
echo $PYTHONPATH
-> /usr/local/google_appengine
This is using the Python SDK version 1.8.0.
My remote API works fine using remote_api_shell.py, but doesn't work when trying to accessing from within a python script. I'm using the sample code from google:
from google.appengine.ext.remote_api import remote_api_stub
import getpass
def auth_func():
return (raw_input('Username:'), getpass.getpass('Password:'))
remote_api_stub.ConfigureRemoteApi(None, '/_ah/remote_api', auth_func,
'localhost:8080')
and I'm also importing the fix_sys_path() from dev_appserver.py to set my sys.path correctly for the google app engine SDK:
import dev_appserver
dev_appserver.fix_sys_path()
that adds, among other paths, the following line to my sys.path:
'/google_appengine_1.8.0/lib/fancy_urllib'
However, the following error is thrown when the above call to remote_api_stub.ConfigureRemoteApi() is called:
opener.add_handler(fancy_urllib.FancyProxyHandler())
AttributeError: 'module' object has no attribute 'FancyProxyHandler'
Wait 7 years, and it all seems to work fine.
I am getting this error when running the app in production. It works fine in my local environment. But, after I upload to server, it throws this exception.
The test_handler module exists in test package (and it works fine in local). I believe that the entire folder structure will be uploaded to the server, when we upload.
Also, is there a way to see the entire folder structure in the Google Application Account online?
============================================================================
<type 'exceptions.ImportError'>: cannot import name test_handler
Traceback (most recent call last):
File "/base/data/home/apps/mad-scribe/1.346944987034829366/url_handler.py", line 15, in <module>
from test import test_handler
You can now download the source if you were the developer who uploaded it.
This should give you a concrete understanding of what was uploaded.
AppEngine Docs: Downloading Source Code
The issue was with the package name. I named the package as 'test', which might have stepped on the shoes of some other package.
from test import test_handler
Renaming the package name (test -> test_handlers) fixed the issue.
Thanks to all who responded to this question.