"ImportError: No module named _ssl" with dev_appserver.py from Google App Engine - google-app-engine

Background
"In the Python runtime, we've added support for the Python SSL
Library, so you can now open secure connections to remote services
such as Apple's Push Notification service."
This quote is taken from a recent post on the Google App Engine blog.
Implementation
If you want to use native python ssl, you must enable it using the libraries configuration in your application's app.yaml file where you specify the library name "ssl" . . .
These instructions are provided for developers through the Google App Engine documentation.
The following lines have been added to the app.yaml file:
libraries:
- name: ssl
version: latest
This much is in line with the advice provided through the Google App Engine documentation.
Problem
I have tried running my project in three different configurations. Two are working, and one is not.
Working ...
After I upload my application to Google App Engine, and run my project through the live server, everything works fine.
Working ...
When I run my project with manage.py runserver and include the Google App Engine SKD in my PYTHONPATH, everything works fine.
Not Working ...
However, when I run my project with dev_appserver.py, I get the following error:
ImportError at /
No module named _ssl
Request Method: GET
Request URL: http://localhost:8080/
Django Version: 1.4.3
Exception Type: ImportError
Exception Value:
No module named _ssl
Exception Location: /usr/local/lib/google_appengine_1.7.7/google/appengine/tools/devappserver2/python/sandbox.py in load_module, line 856
Python Executable: /home/rbose85/Code/venvs/appserver/bin/python
Python Version: 2.7.3
Python Path:
['/home/rbose85/Code/product/site',
'/usr/local/lib/google_appengine_1.7.7',
'/usr/local/lib/google_appengine_1.7.7/lib/protorpc',
'/usr/local/lib/google_appengine_1.7.7',
'/usr/local/lib/google_appengine_1.7.7',
'/usr/local/lib/google_appengine_1.7.7/lib/protorpc',
'/usr/local/lib/google_appengine_1.7.7',
'/usr/local/lib/google_appengine_1.7.7/lib/protorpc',
'/home/rbose85/Code/venvs/appserver/lib/python2.7',
'/home/rbose85/Code/venvs/appserver/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/local/lib/google_appengine',
u'/usr/local/lib/google_appengine_1.7.7/lib/django-1.4',
u'/usr/local/lib/google_appengine_1.7.7/lib/ssl-2.7',
u'/usr/local/lib/google_appengine_1.7.7/lib/webapp2-2.3',
u'/usr/local/lib/google_appengine_1.7.7/lib/webob-1.1.1',
u'/usr/local/lib/google_appengine_1.7.7/lib/yaml-3.10']
Server time: Wed, 24 Apr 2013 11:23:49 +0000

For the current GAE version (1.8.0 at least until 1.8.3), if you want to be able to debug SSL connections in your development environment, you will need to tweak a little bit the gae sandbox:
add "_ssl" and "_socket" keys to the dictionary _WHITE_LIST_C_MODULES in /path-to-gae-sdk/google/appengine/tools/devappserver2/python/sandbox.py
Replace the socket.py file provided by google in /path-to-gae-sdk/google/appengine/dis27 from the socket.py file from your Python framework.
IMPORTANT: Tweaking the sandbox environment might end up with functionality working on your local machine but not in production (for example, GAE only supports outbound sockets in production). I will recommend you to restore your sandbox when you are done developing that specific part of your app.

The solution by jmg works, but instead of changing the sdk files, you could monkey patch the relevant modules.
Just put something like this on the beginning of your project setup.
# Just taking flask as an example
app = Flask('myapp')
if environment == 'DEV':
import sys
from google.appengine.tools.devappserver2.python import sandbox
sandbox._WHITE_LIST_C_MODULES += ['_ssl', '_socket']
from lib import copy_of_stdlib_socket.py as patched_socket
sys.modules['socket'] = patched_socket
socket = patched_socket

I had to use a slightly different approach to get this working in CircleCI (unsure what peculiarity about their venv config caused this):
appengine_config.py
import os
if os.environ.get('SERVER_SOFTWARE', '').startswith('Development'):
import imp
import os.path
import inspect
from google.appengine.tools.devappserver2.python import sandbox
sandbox._WHITE_LIST_C_MODULES += ['_ssl', '_socket']
# Use the system socket.
real_os_src_path = os.path.realpath(inspect.getsourcefile(os))
psocket = os.path.join(os.path.dirname(real_os_src_path), 'socket.py')
imp.load_source('socket', psocket)

I had this problem because I wasn't vendoring ssl in my app.yaml file. I know the OP did that, but for those landing here for the OP's error, it's worth making sure lines like the following are in your app.yaml file:
libraries:
- name: ssl
version: latest

Stumbled upon this thread trying to work with Apples Push notification service and appengine... I was able to get this working without any monkey patching, by adding the SSL library in my app.yaml, as recommended in the official docs, hope that helps someone else :)

I added the code to appengine_config.py as listed by Spain Train, but had to also add the following code as well to get this to work:
phttplib = os.path.join(os.path.dirname(real_os_src_path), 'httplib.py')
imp.load_source('httplib', phttplib)

You can test if ssl is available at your local system by opening a python shell and typing import ssl. If no error appears then the problem is something else, otherwise you don't have the relevant libraries installed on your system. If you are using a Linux operating system try sudo apt-get install openssl openssl-devel or the relevant instructions for your operating system to install them locally. If you are using windows, these are the instructions.

Related

Why does my Google App Engine app use an old version of OpenSSL?

I have
libraries:
- name: ssl
version: latest
In my app.yaml file
I print out
print(ssl.OPENSSL_VERSION)
In my main.py file, which is a flask app. When I run main.py using python, the program prints out
'OpenSSL 1.1.0h 27 Mar 2018'
But when I run the app using dev_appserver.py, it prints out
OpenSSL 0.9.8zh 14 Jan 2016
Why is it using an older version of open ssl? I think this is leading to this error that I get when I try to send a message using Twilio
TwilioRestException: HTTP 426 error: Unable to create record: Upgrade Required
I'm not sure, but I think the gcloud tools don't install any version of OpenSSL on your computer, and instead use the version of OpenSSL that is already there as part of your existing Python installation.
The version of OpenSSL used with dev_appserver.py thus wouldn't be the same one that is used in production.
The difference between main.py and dev_appserver.py is likely caused by different different path configurations. Those two versions of OpenSSL are somewhere on your computer and the path configuration picks up one versus the other.
Best way to check is to log the OpenSSL version in your production environment.

Why is dev_appserver.py not working with new google cloud endpoints lib?

I am using Windows 7 and creating APIs on the standard environment Cloud Endpoints Framework in Python. When I run command dev_appserver.py app.yaml and go to http://localhost:8080/_ah/api/explorer to test my API I get an ImportError with the last lines being:
File "C:\Python27\lib\platform.py", line 632, in win32_ver from _winreg
import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
File "C:\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\
google\appengine\tools\devappserver2\python\sandbox.py", line 964, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named _winreg
When I deploy the API to google cloud it works fine. Also, when I use an older version of this library pip install -t lib google-endpoints --extra-index-url=https://gapi-pypi.appspot.com/admin/nurpc-dev --ignore-installed in my application, the dev_appserver works as expected. I think it's something to do with an update to this library?
Any help would be great.
try updating your libraries again, as new versions have come out recently. See if that fixes your issue!
This opened issue with the Google team explains a work around before a fix is released. Work around is also shown in this answer:
- Go to: <sdk_root>\google\appengine\tools\devappserver2\python\sandbox.py
- Find the definition of _WHITE_LIST_C_MODULES = [xxx]
- Add the following two lines to the list: '_winreg', '_ctypes',

pycrypto and Google App Engine on windows 10

I'm trying to run pycrypto (2.6) on google app engine (1.9.32) with python 2.7 on windows 10.
I've added the following to my app.yaml:
libraries:
- name: pycrypto
version: "2.6"
and added the following config:
import os
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
But I'm still getting the following error:
The Python egg cache directory is currently set to:
/tmp
Perhaps your account does not have write access to this directory?
You can
change the cache directory by setting the PYTHON_EGG_CACHE environment
variable to point to an accessible directory.
pycrypto works fine on windows 10 in python command line. The problem is just in the Google app engine.
How is it possible to work around this?
2 side notes:
The phenomenon is known as the Python egg error.
Windows 10 folder locking in read-only is documented. No solutions seemed to work so far.

How to run managed-vm-gae example code locally

I followed this tutorial
to get a Bigtable client up and running in Google Managed VMs. But is there a way to run this locally? Reason is that deploying the code remotely in development is a pain.
Normally I can use dev_appserver.sh to run GAE app locally. But when I run it, I'm getting this error:
Caused by: java.lang.IllegalStateException: Jetty ALPN has not been
properly configured.
Which means we need to include ALPN library? Since our codebase is in Java 7, I used this ALPN version: 7.1.3.v20150130.
I then tried again with this:
dev_appserver.sh --jvm_flag=-Xbootclasspath/p:/Users/shouguoli/tmp/alpn-boot-7.1.3.v20150130.jar
still getting this error:
Caused by: com.google.apphosting.api.ApiProxy$CallNotFoundException:
The API package 'urlfetch' or call 'Fetch()' was not found.
How do you get it to work locally?
The sample was updated last week. It's based on the java 8 compat runtime, which means that you have access to most of the App Engine API's including Users, Task Queues, and Datastore.
There is a new Netty TCNative module that uses Boring SSL.
To use it with the pom.xml in the sample, do:
mvn clean -Pmac jetty:run -Dbigtable.projectID=<your-project> -Dbigtable.clusterID=<your-cluster> -Dbigtable.zone=<your-zone>
To use on Windows, use -Pwindows instead of -Pmac. For linux, omit the Profile -P as it's the default.
To deploy:
mvn clean gcloud:deploy -Dbigtable.projectID=<your-project> -Dbigtable.clusterID=<your-cluster> -Dbigtable.zone=<your-zone>
NOTE - it is advisable to do the clean between running locally and running remotely as the TCNative module is currently specific to the platform the code runs on.
We are in the process of updating all of our samples to use TCNative, we hope to have this by 3/10/16.

Why am I getting: EventError: libraries entries only supported by python27 runtime? when running GAE

I'm running a local development copy of Google AppEngine PHP SDK v1.8.0, with PHP v5.4.3 and Python v2.7, all under Windows 7 64bit.
I have followed instructions from a number of posts both here and elsewhere in order to register Python with my OS and to properly install the Python PIL module.
The last post I found here Unable to find the Python PIL library.Google App Engine. I have therefore added :
libraries:
- name: PIL
version: 1.1.7
to app.yaml.
I now receive the error quoted in the title of this post. If anyone can advise, I would be grateful.
Thankyou.
I think what #Tim was trying to say is that you should change your app.yaml to say "runtime: python27" instead of "runtime: php" since, as the error suggests, the PIL library is only supported by python27, not php.

Resources