Google App Engine local development server SSL warning (Python 2.7) - python

Each time I upload my app to Google App Engine, the logs always show this warning:
WARNING appengine_rpc.py:435 ssl module not found. Without the ssl
module, the identity of the remote host cannot be verified, and
connections may NOT be secure. To fix this, please install the ssl
module from http://pypi.python.org/pypi/ssl .
I'm running a virtualenv with Python 2.7. When I'm in it, I try to run
$ pip install ssl
but this produces an error:
ValueError: This extension should not be used with Python 2.6 or later
(already built in), and has not been tested with Python 2.3.4 or earlier.
If ssl is built in to Python 2.7, how do I tell the local development server to use the built in ssl module?

It sounds like appcfg.py is not using your virtualenv correctly. You can try editing the appcfg.py script so that it prints sys.version and sys.path, to confirm that it's using your virtualenv correctly. (The shebang line is "#!/usr/bin/env python", so that should use your active environment, but it's worth checking.)
If you're using the Launcher, you need to tell it explicitly where to look for your virtualenv's Python. Go to Preferences, and set the Python Path to your virtualenv's Python 2.7. The Launcher prints which Python path it is using in the Logs (at least when starting the dev server).

Related

How to solve Local path <path> is not registered within uploads in the request in PyCharm 2022.2.1 (Professional Edition)?

I want to set up a Django project with docker-compose and PyCharm on my PC with Ubuntu 22.04 OS. Using PyCharm 2022.2.1 (Professional) I get the following error
How to solve Local path is not registered within uploads in the request
I added a Python interpreter from Settings > project > Python interpreter and then add interpreter > on SSH after that entered ssh credentials and on the system interpreter finally I created the Python interpreter.
I have docker-compose run on another terminal.
After I run the runserver command it shows this error:
this is the runserver command configuration:
I have recreated the interpreter, and explored the same problems on JetBrains website but couldn't solve the issue.
This bug was reported as PY-55396 on the JetBrains bug tracker.
The bug was solved in PyCharm 2022.2.2, the solution is to upgrade to that version or downgrade to PyCharm 2021.3.

Executing a Python script with PHP using Apache server

I'm experiencing an odd issue. I have a Python script that I'm calling from a PHP script. This is all running on an Apache server on Ubuntu 18.04. Part of the Python script uses the Google Drive API. EDIT: See the bottom After a lot of testing and replication,I've concluded that simply having the following Google Drive Python libraries and dependencies imported:
from __future__ import print_function
from googleapiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
from googleapiclient.http import MediaIoBaseDownload
Messes up the script when it is called from the web. By that I mean the Python script does not seem to execute, and any shell output that I should be getting through print statements do not make their way back to the PHP script. When calling the script locally (python myscript.py), it works just fine.
The weird part is that when I remove these import statements from the Python script, it executes just fine from both PHP, and from directly launching the script from a browser. In both of those cases I am also able to get the shell output back to the PHP script. I have given the proper permissions for the Python script, and I have configured Apache to be able run CGI scripts. Here is what my Python script looks like:
#!/usr/bin/env python3
import cgitb
from __future__ import print_function
from googleapiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
from googleapiclient.http import MediaIoBaseDownload
cgitb.enable()
print("Hello World")
And here is what my PHP script looks like:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$output = shell_exec("python path_to_my_script/myscript.py");
echo $output;
?>
EDIT:
I ran a couple of tests using another 3rd party Python library that I installed with pip, and this actually seems to be an issue with importing any 3rd party library when executing the script via PHP.
Your issue is a user permissions issue. You should run the Apache server as the user that owns your python environment, or allow the user running the server execute permissions in your python environment.
This answer should help you figure out your best setup for the server.
Also read about Apache VirtualHosts Configuration
I Figured this out. Hopefully this will help anyone with a similar issue. When you execute a Python or shell script from a PHP script served on an Apache server, the script will be ran as the user, www-data by default.
By running the command sudo -u www-data (running the following command as the www-data user) python myscript.py, I got a traceback error that exclaimed that the 3rd party modules could not be found. What's happening is that by default pip (python package manager) installs its packages on a user level basis. That's fine for most applications, but causes a problem in this case as the www-data user understandably cannot find the installed packages, and the script crashes. This becomes particularly tricky from the perspective of the PHP script, as it doesn't understand this and the output is just null.
I understand that installing pip packages with sudo is not preferred as it can cause conflict issues in some cases; however, this is the easiest way to solve the problem for most packages. For cases where this does not work, check out this answer from infinigrove:
How to install Python Package for global use by all users (incl. www-data)
Finally, from what I could see in order to install a pip package as the www-data user (sudo -u www-data pip install package), www-data would have to be granted sudo permissions, which is definitely not a recommended solution.

Develop and Debug Python with Remote Server

There is a Ubuntu server with Python 3.6 and many other required Python modules installed.
When using Eclipse for Python development, can I connect Eclipse to the full Python environment installed on the Ubuntu server?
I don't want to install the whole Python environment in the local PC client.
Thanks.
Latest Update:
I have installed the latest Eclipse (2018-12) and PyDev (7.0.3) plugin and Python (3.6.7) on Windows 10. And I have also created a test file called Test.py with the following content:
import my_server_module as my;
print(my.version);
It doesn't work at all because the "my_server_module" is not installed on Windows 10, it just installed on the Ubuntun server.
How can I debug the Test.py with connecting to the python environment on Ubuntu server?
Always use the virtualenv for python app and load into the eclipse - https://www.michaelpollmeier.com/eclipse-pydev-and-virtualenv
It seems it is impossible for remote debugging.
I have installed all the modules in local PC for python development. This is the only way.

Google App Engine (Python 2.7) Win 10 ImportError: No module named appengine.api

I have a problem trying to run a python project, I already installed Python 2.7 and Google Cloud SDK (I followed up the instructions in https://cloud.google.com/appengine/downloads#Google_App_Engine_SDK_for_Python), but I get the following error:
ImportError: No module named appengine.api
Also I set up the enviroment variables:
Enviromental variables
Windows Location
My OS is Windows 10
If you execute the project in local, you would get that error(google.appengine.api error). Basically it has to be executed on a server.
The server can be started using your terminal.
1) Go to the project path(root folder of all files in the project,eg: appengine)
2) start the server using $ dev_appserver.py app.yaml. It starts server at localhost port 8000 as the default one.
3) In the server start depends on the handler and its path specified (like '/' or '/testjob') try localhost:8000/ or localhost:8000/testjob
4) All the logs written in the program will be shown in the terminal. For logs try using 'logging' module , make sure to mention the logging level else basic level logs are not shown

Django fails to find R when loading RPy2 under virtualenv

This is kind of confusing, apologies in advance. I am configuring a new (temporary) internal production web server with the following specs:
OSX 10.8.4 (Mountain Lion), Apache 2.2.25, PostgreSQL 9.2, Python 2.7.5, Django 1.5.2, R 3.0.1, RPy2 2.3.7, virtualenv 1.10.4, virtualenvwrapper 4.1.1, mod_wsgi 3.4
This was set up on a new machine using Homebrew and pip. I removed original, Apple-installed versions of Apache and Postgres before doing the installs. Both Apache and mod_wsgi work. mod_wsgi is installed to run as daemon process.
Virtual hosts is configured in Apache to access django via mod_wsgi and the wsgi.py file contains the following lines:
import os,sys,site
site.addsitedir('/Users/kaiju/.virtualenvs/nauru_dev/lib/python2.7/site-packages')
# Activate the virtual environment
# Taken from http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/
activate_env = os.path.expanduser('/Users/kaiju/.virtualenvs/nauru_dev/bin/activate_this.py')
execfile(activate_env,dict(__file__=activate_env))
All python packages (including django and RPy2) have been installed using the virtual environment's python site-packages.
R was installed using Homebrew and works fine from the command line or from python interpreter when invoked within the virtual env (most of the time, more later).
homebrew.mxcl.httpd.plist has been added to /Library/LaunchDaemons instead of ~/Library/LaunchAgents so that it would automatically start when the machine is rebooted. homebrew.mxcl.postgres.plist has been added to ~/Library/LaunchAgents (probably should also move it to /Library/LaunchDaemons).
So here is the problem. When everything was installed, it worked together fine. To verify everything was cool, I restarted the machine and tested the django application. Apache is running, as is django, but now I get the following error:
RuntimeError at /admin/
R_HOME not defined, and no R command in the PATH.
Request Method: GET
Request URL: http://nauru.xoma.com/admin/
Django Version: 1.5.2
Exception Type: RuntimeError
Exception Value: R_HOME not defined, and no R command in the PATH.
Exception Location: /Users/kaiju/.virtualenvs/nauru_dev/lib/python2.7/site-packages/rpy2/rinterface/__init__.py in <module>, line 48
Python Executable: /usr/local/opt/python/bin/python2.7
Python Version: 2.7.5
Python Path: ['usr/local/var/django/code/ri',
'/usr/local/var/django/code',
'/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.9.8-py2.7.egg',
'/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.4-py2.7.egg',
'/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
'/Library/Python/2.7/site-packages',
'/Users/kaiju/.virtualenvs/nauru_dev/lib/python2.7/site-packages',
'/Users/kaiju/.virtualenvs/nauru_dev/lib/python2.7/site-packages/PIL']
But if I open a terminal window and type
sudo apachectl stop
sudo apachectl start
When the server comes back, everything works. This does not happen if I use sudo apachectl restart, in that case the error remains. What is going on? Too many moving parts for me.
The best way to resolve this issue is to add
os.environ['R_HOME']='/usr/local/Cellar/r/3.0.1/R.framework/Resources'
to the beginning of the wsgi.py file that is being used for mod_wsgi / django. It should be added before activating the virtual environment. Any other environ variables should also be added here. So now the wsgi.py file looks like this:
import os,sys,site
os.environ["R_HOME"]='/usr/local/Cellar/r/3.0.1/R.framework/Resources'
site.addsitedir('/Users/kaiju/.virtualenvs/nauru_dev/lib/python2.7/site-packages')
# Activate the virtual environment
# Taken from http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/
activate_env = os.path.expanduser('/Users/kaiju/.virtualenvs/nauru_dev/bin/activate_this.py')
execfile(activate_env,dict(__file__=activate_env))
sys.path.insert(0,'/usr/local/var/django/code')
sys.path.insert(0,'/usr/local/var/django/code/ri')
# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "ri.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE","ri.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Many thanks to those who contributed to this discussion!
I believe that sys.path has little to do with the environment variable PATH, which the one that should be set (along with LD_LIBRARY_PATH if needed).
I am guessing that Apache is running as a dedicated user (not your own UID for obvious security reasons), and that user does not have R in its path (other security step - by default Apache should not be able to run any executable on your system).
You'll have to either allow Apache to run the system's R, or have an R install only for your server (the later being the better if considering anything beyond a prototype to play with)

Categories

Resources