Google App Engine Cron Job failing with following error - python-2.7

Same code deployed on development system works fine but once deployed to production fails with following error:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
ImportError: No module named urlhandlers
Following is the URL handler code:
import traceback
import datetime
from google.appengine.api import taskqueue
import re
import webapp2
from utilities.logger import logThis, AEL_LEVEL_INFO, AEL_LEVEL_CRITICAL
from constants.constants import GAET_MASTERTASK_NAME, GAEQ_FOR_MASTERTASK
import sys
class QueueAMasterTask(webapp2.RequestHandler):
def get(self):
try:
dt =datetime.datetime.now()
logThis(AEL_LEVEL_INFO, 'Master Task added to its Q at[%s]' %(dt))
task_name = GAET_MASTERTASK_NAME + str(datetime.datetime.now())
task_name = re.sub('[^a-zA-Z0-9_-]', '_', task_name)
taskqueue.add(queue_name=GAEQ_FOR_MASTERTASK,name=task_name)
logThis(AEL_LEVEL_INFO, "OK-MASTER TASK ADD")
except taskqueue.DuplicateTaskNameError:
logThis(AEL_LEVEL_CRITICAL, "EXCEPTION on QueueAMasterTask-" + traceback.format_exc())
except:
logThis(AEL_LEVEL_CRITICAL, "EXP on QueueAMasterTask-" + traceback.format_exc())
app = webapp2.WSGIApplication([('/QueueAMasterTask', QueueAMasterTask)
], debug=True)
Cron job error log

Related

sqlalchemy.exc.UnboundExecutionError: MetaData object is not bound to an Engine or Connection

(env) C:\Users\recur\Desktop\project\app>python manage.py
Traceback (most recent call last):
File "C:\Users\recur\Desktop\project\app\manage.py", line 1, in <module>
from main import app
File "C:\Users\recur\Desktop\project\app\main\__init__.py", line 47, in <module>
Base.metadata.create_all(bind=engine)
File "C:\Users\recur\Desktop\project\env\lib\site-packages\sqlalchemy\sql\schema.py", line 4888, in create_all
bind = _bind_or_error(self)
File "C:\Users\recur\Desktop\project\env\lib\site-packages\sqlalchemy\sql\base.py", line 1658, in _bind_or_error
raise exc.UnboundExecutionError(msg)
sqlalchemy.exc.UnboundExecutionError: MetaData object is not bound to an Engine or Connection. Execution can not proceed without a database to execute against.
What am I doing wrong?
init.py
from .config import Config
engine = Config.SQLALCHEMY_DATABASE_URI
Base = declarative_base()
Base.query = session.query_property()
Base.metadata.create_all(bind=engine)
config.py
import os
import json
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY')
SQLALCHEMY_DATABASE_URI=os.environ.get('DATABASE_URI')
manage.py
from main import app
if name == 'main':
app.run(host='127.0.0.1', port='5000')

Behave ImportError: No module named features.steps.pages.home_page

I have a sample BDD scenario in Python Behave. When i run the feature I get the error:
ImportError: No module named features.steps.pages.home_page
I am not sure why it is complaining. home_page.py is in the pages folder, pages is in the steps folder and steps folder is in the features folder.
In pages folder I have an init.py file.
Why is it complaining it cannot find home_page.py?
My code is: features\steps.py
from behave import *
#from features.steps.pages import home_page
from features.steps.pages.home_page import HomePage
#from features.steps.pages import search_page
from features.steps.pages.search_page import SearchPage
from features.steps.pages import home_page
#Given ('we are on the homepage')
def step(context):
context.browser.get('http://www.test.com')
#When ('we enter "{product}" in the search field')
def step(context, product):
#search_field = context.browser.find_element(By.XPATH, 'id("twotabsearchtextbox")')
#search_field.send_keys(product)
home_page = HomePage(context)
home_page.enter_product_in_search_field(product, context)
#When ('And we click the search button')
def step(context):
#search_button = context.browser.find_element(By.XPATH, './/*[#id="nav-search"]/form/div[2]/div/input')
searchPage_results = home_page.click_search_button(context)
#search_button.click()
#Then ('the list of products are displayed')
def step(context):
context.searchPage_results.search_products_results(context)
#wait = WebDriverWait(context.browser, 60)
#divs = wait.until(EC.presence_of_all_elements_located((By.XPATH, '//div/a/h2')))
#for i in divs:
#div2 = divs + '/a/h2'
#print divs.get_attribute('value')
#print divs
#print i.text
#print "i"
# divs
features\steps\pages\home_page.py
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from search_page import SearchPage
class HomePage(object):
def __init__(self, context):
context = context
def enter_product_in_search_field(self, product, context):
search_field = context.browser.find_element(By.XPATH, 'id("twotabsearchtextbox")')
search_field.send_keys(product)
return self
def click_search_button(self, context):
search_button = context.find_element(By.XPATH, './/*[#id="nav-search"]/form/div[2]/div/input').click()
return SearchPage(context)
features\test_feature.feature
Feature: testing product
Scenario Outline: visit test and search for product
Given we are on the test homepage
When we enter "<product>" in the search field
And we click the search button
Then the list of products are displayed
Examples: By product
| Forumla One |
| PS4 |
| Headphones |
My directory structure is:
E:features\test_feature.feature
E:features\init.py
E:features\pages\init.py
E:features\pages\home_page.py
E:features\pages\search_page.py
The full error is:
E:\RL Fusion\projects\BDD\Python BDD\PythonBDD\Selenium Sample\features>behave test_feature.feature
Exception ImportError: No module named features.steps.pages.home_page
Traceback (most recent call last):
File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Python27\scripts\behave.exe\__main__.py", line 9, in <module>
File "C:\Python27\lib\site-packages\behave\__main__.py", line 109, in main
failed = runner.run()
File "C:\Python27\lib\site-packages\behave\runner.py", line 672, in run
return self.run_with_paths()
File "C:\Python27\lib\site-packages\behave\runner.py", line 678, in run_with_paths
self.load_step_definitions()
File "C:\Python27\lib\site-packages\behave\runner.py", line 658, in load_step_definitions
exec_file(os.path.join(path, name), step_module_globals)
File "C:\Python27\lib\site-packages\behave\runner.py", line 304, in exec_file
exec(code, globals, locals)
File "steps\amazon_steps.py", line 6, in <module>
from features.steps.pages.home_page import HomePage
ImportError: No module named features.steps.pages.home_page
How can I resolve this issue?
Thanks, Riaz
It looks like you are not importing your modules correctly. To turn a directory in to a module, change all your init.py files to __init__.py.
Then when you are importing in features/steps.py you can use:
from pages.home_page import HomePage

How to use Flask-APScheduler in existed Flask app

I am trying to get familiar with Flask-APScheduler plug-in through the following sample code: https://github.com/viniciuschiele/flask-apscheduler/blob/master/examples/jobs.py#L1
My project has the following structure:
backend
run.py
application
__init__.py
utilities
__init__.py
views
models
where,
backend>run.py is:
from application import app
app.run(debug=True)
from application import scheduler
scheduler.start()
backend>application>__init__.py is:
from flask import Flask
app = Flask(__name__)
from application.utilities.views import Config
from flask_apscheduler import APScheduler
app.config.from_object(Config())
scheduler = APScheduler()
scheduler.init_app(app)
backend>application>utilities>__init__.py is empty
backend>application>utilities>models.py is empty
backend>application>utilities>views.py is:
class Config(object):
JOBS = [
{
'id': 'job1',
'func': 'application:utilities:views:job1',
'args': (1, 2),
'trigger': {
'type': 'cron',
'second': 10
}
}
]
def job1(a, b):
print(str(a) + ' ' + str(b))
However, I get the following error:
(env)$ python run.py local
Traceback (most recent call last):
File "run.py", line 1, in <module>
from application import app
File "HOME/backend/application/__init__.py", line 106, in <module>
scheduler.init_app(app)
File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/flask_apscheduler/scheduler.py", line 73, in init_app
self.__load_jobs(app)
File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/flask_apscheduler/scheduler.py", line 136, in __load_jobs
self.__load_job(job, app)
File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/flask_apscheduler/scheduler.py", line 159, in __load_job
func = ref_to_obj(func)
File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/apscheduler/util.py", line 264, in ref_to_obj
raise LookupError('Error resolving reference %s: error looking up object' % ref)
LookupError: Error resolving reference application:utilities:views:job1: error looking up object
Does my structure look like ok? Have a placed the right code in the right place? What should I change to make it work?
Your reference should only have one colon (":"). The colon separates the required import from the variable that has to be looked up. So:
'func': 'application.utilities.views:job1'

error with Firefox Binary 'The browser appears to have exited'

I have problem with selenium and Firefox when i run my code below:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import urllib2,cookielib
import re
from pyvirtualdisplay import Display
import urllib
import urllib2
import httplib2
from os import system
display = Display(visible=0, size=(800, 600))
display.start()
name = 'test'
driver = webdriver.Firefox()
driver.get("http://example.com")
driver.switch_to_default_content()
inputElement = driver.find_element_by_name("name")
inputElement.send_keys(name)
inputElement.submit()
print "Please wait for 5s to get results.."
content = driver.page_source.encode("utf-8")
html = str(content)
soup = BeautifulSoup(html)
print soup
display.stop()
driver.close()
And i get this error:
Exception in thread command9:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
File "/usr/local/lib/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
File "plugins/help_plugin.py", line 165, in handler_search
search_number_ar(type, source, parameters)
File "plugins/test.py", line 230, in search_test
driver = webdriver.Firefox()
File "/usr/local/lib/python2.7/site-packages/selenium-2.45.0-py2.7.egg/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
self.binary, timeout),
File "/usr/local/lib/python2.7/site-packages/selenium-2.45.0-py2.7.egg/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
self.binary.launch_browser(self.profile)
File "/usr/local/lib/python2.7/site-packages/selenium-2.45.0-py2.7.egg/selenium/webdriver/firefox/firefox_binary.py", line 66, in launch_browser
self._wait_until_connectable()
File "/usr/local/lib/python2.7/site-packages/selenium-2.45.0-py2.7.egg/selenium/webdriver/firefox/firefox_binary.py", line 100, in _wait_until_connectable
raise WebDriverException("The browser appears to have exited "
WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
my code running in vps and i use selenium-2.45.0 and firefox.x86_64-31.6.0.
I try alot of ways to solve my problem but nothing working with me.
I try to update selenium and i try to downgrade firefox and alot of solution.
Just to know when i run my code for the first time it work but when i run it again it show me the above error.
I hope someone help.

Export django settings to my python file

I am trying to make logparser.py in django project which parses the data coming from different servers.
And on running the command on terminal :
$ python logparser.py
This error is coming :
Traceback (most recent call last):
File "logparser.py", line 13, in <module>
SMTP_CONF = settings.SMTP_CONF
File "/home/arya/.virtualenv/Devel/.virtualenvs/hu/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/home/arya/.virtualenv/Devel/.virtualenvs/hu/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
File "/home/arya/.virtualenv/Devel/.virtualenvs/hu/local/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'hma.settings' (Is it on sys.path?): No module named hma.settings
my logparser.py contains:
import re
import os
import fnmatch
import gzip
import bz2
from collections import defaultdict
from django.core.mail import send_mail
from django.core.mail.backends import smtp
import smtplib
from email.mime.text import MIMEText
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hma.settings")
from django.conf import settings
SMTP_CONF = settings.SMTP_CONF
def send_email(self,fromaddress,toaddresses,content,subject):
smtp_server = SMTP_CONF["SERVER"]
smtp_username = SMTP_CONF["USERNAME"]
smtp_password = SMTP_CONF["PASSWORD"]
smtp_port = SMTP_CONF["PORT"]
msg = MIMEText(content, 'html', _charset='utf-8')
msg['Subject'] ='Alert message for bad and internal server error'
msg['From'] = fromaddress
msg['To'] = toaddresses
server = smtplib.SMTP(smtp_server,smtp_port)
server.starttls()
server.login(smtp_username,smtp_password)
server.send_mail(fromaddress,toaddresses,msg.as_string())
server.quit()
return True
I know I am doing wrong something with command [python manage.py], but i need to run like this. Any solution for this exporting django settings to separate python file??
Well, This is the exact Usecase why Django provided an ability to create custom commands. You can use all the features of django, in your script, Its like your script will be running inside a Django Container. Here is the Documentation https://docs.djangoproject.com/en/dev/howto/custom-management-commands/.
In case you don't want to use custom management commands though there is also a simple way to run your code within Django's context. Simply put the following at the beginning of your python file, you want to run:
from django.conf import settings
from django.core import management
management.setup_environ(settings)