Problems importing from Google App Engine SDK in PyCharm Pro - python

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

Related

Unable to connect to Python on my own computer in Power BI

I'm attempting to figure out the mechanics of running Python scripts in Power BI for Reasons and I've hit a snag. I was running through the steps in this somewhat basic tutorial and I came to the section in which I am supposed to paste the script into they Python Script screen, which is Step 3 of the 'Run the Script and Import Data' section.
When I followed the steps, which are essentially to paste the example script into the window and hit Okay, I got this 'helpful' error:
`
Details: "ADO.NET: Python script error.
<pi>Traceback (most recent call last):
File "C:\Users\my.username\PythonScriptWrapper_aca634c7-30c3-4bf4-881b-d1e47bb0a919\PythonScriptWrapper.PY", line 2, in <module>
import os, pandas, matplotlib
File "C:\Users\my.username\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 109, in <module>
from . import _api, _version, cbook, docstring, rcsetup
File "C:\Users\my.username\Anaconda3\lib\site-packages\matplotlib\rcsetup.py", line 27, in <module>
from matplotlib.colors import Colormap, is_color_like
File "C:\Users\my.username\Anaconda3\lib\site-packages\matplotlib\colors.py", line 51, in <module>
from PIL import Image
File "C:\Users\my.username\Anaconda3\lib\site-packages\PIL\Image.py", line 89, in <module>
from . import _imaging as core
ImportError: DLL load failed while importing _imaging: The specified module could not be found.
</pi>"
`
I have verified that the matplotlib and pandas packages were installed via pip list, os doesn't show up, which surprised me but I know it's part of the standard library so I'm not stressing about it unless someone thinks I should. Is anyone an expert in this? Is there a better way? Am I doomed to scream into the Power BI void for all eternity?
The somewhat basic tutorial is based on using standard python from python.org. However, you are using the Anaconda distribution, which basically requires the environment to be activated before any modules - especially pandas' C-library - can be accessed.
You can achieve that in the cmd shell by running
conda activate
C:\Users\my.username\AppData\Local\Microsoft\WindowsApps\PBIDesktopStore.exe
which assumes that you are using the Power BI Desktop version from the Microsoft store.

ModuleNotFoundError: No module named 'tasklets'

I'm playing with GAE but I realized that when I try to start the .py file I get this error. What can I do?
Thanks
python3 test.py runserver
Traceback (most recent call last):
File "test.py", line 6, in <module>
from google.appengine.ext import ndb
File "/usr/local/lib/python3.6/dist-packages/google/appengine/ext/ndb/__init__.py", line 7, in <module>
from tasklets import *
ModuleNotFoundError: No module named 'tasklets'
The NDB library is only available in the standard environment GAE, which only works with python 2.7.
So either switch to the standard environment (and python 2.7) or use the flexible environment instructions for Using Cloud Datastore.
Maybe of interest: How to tell if a Google App Engine documentation page applies to the standard or the flexible environment

Initializing AWS for rails app via cli error

I am trying to deploy my rails app to AWS Beanstalk using the AWS CLI and it looks like when I am trying to initialize my repo I get an error because I don't have python installed? My question is, do I need python installed for this to work? Why would I need python if my app was developed in Ruby, or is this related to how Amazon built the tool?
$eb init
Traceback (most recent call last):
File "/usr/local/bin/eb", line 5, in <module>
from pkg_resources import load_entry_point
File "/Library/Python/2.7/site-packages/pkg_resources/__init__.py", line 3131, in <module>
#_call_aside
File "/Library/Python/2.7/site-packages/pkg_resources/__init__.py", line 3117, in _call_aside
The AWS website states the requirements:
The EB CLI is developed in Python and requires Python version 2.7 or 3.4 in order to run.
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install.html
The CLI has nothing to do with Ruby or Rails, it's just that Amazon chose to code their tool in Python so you will need python to use it.

Testing Google App Engine apps

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

Error when importing WSGIHandler with django

Without any code changing, my django app started throwing an exception while loading the WSGI script. I'm using django 1.3 with python 2.7, and the top-level .wsgi is essentially unmodified from the default:
import os
import sys
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'api.settings'
application = WSGIHandler()
It started producing these errors on any request, as reported by Apache:
mod_wsgi (pid=3283): Target WSGI script '/home/beder/webapps/api/api.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=3283): Exception occurred processing WSGI script '/home/beder/webapps/api/api.wsgi'.
Traceback (most recent call last):
File "/home/beder/webapps/api/api.wsgi", line 4, in <module>
from django.core.handlers.wsgi import WSGIHandler
File "/home/beder/webapps/api/lib/python2.7/django/core/handlers/wsgi.py", line 10, in <module>
from django import http
File "/home/beder/webapps/api/lib/python2.7/django/http/__init__.py", line 122, in <module>
from django.utils.http import cookie_date
File "/home/beder/webapps/api/lib/python2.7/django/utils/http.py", line 7, in <module>
from email.Utils import formatdate
File "/usr/local/lib/python2.7/email/__init__.py", line 79, in __getattr__
__import__(self.__name__)
File "/usr/local/lib/python2.7/email/utils.py", line 27, in <module>
import random
File "/usr/local/lib/python2.7/random.py", line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom
I restarted the server, and it now works normally (no errors). I'm at a loss for what to do - I want to make sure this doesn't happen again, but it's not happening now, and I have no idea why that import error appeared.
Are you executing Django inside a virtualenv? Did you updated or upgraded your system? If so and you upgraded to Python 2.7 from Python 2.6 for example you need to regenerate the virtualenv:
$ virtualenv [your-options] [your-django-project-directory]
For anyone using webfaction like Jesse and myself to run his django app but is not using virtualenv, it can happen that after the upgrade, the custom apache installation for the webapp was never restarted. This means that the stdlib changed but apache still uses python 2.7.2 because its the version it has loaded into memory.
The solution in this case is simple: login to your account via ssh and execute:
[username#webXX ~]$ ~/webapps/<webapp-name>/apache2/bin/restart
This restarts the web server and causes apache to reload the new interpreter binary.
I also found the same error when I updated Python 2.6.5 to 2.6.8, building from source. I also had built mod_wsgi from source, and initially forgot to recompile mod_wsgi for the new version of Python, and that led to the same error concerning urandom.

Categories

Resources