zappa handler not import django app - python

I deploye aws lambda django application with help of zappa. My project works fine locally with wsgi but when I deploy on aws lambda it raise error.
Any help?
zappa tail:
:`Calling tail for stage new2..
Warning! AWS Lambda may not be available in this AWS Region!
Warning! AWS API Gateway may not be available in this AWS Region!
[1496566100097] [INFO] 2017-06-04T08:48:20.97Z 8e08e84e-4902-11e7-9744-b7104a9a6ab2 Detected environment to be AWS Lambda. Using synchronous HTTP transport.
[1496566100160] No module named accounts: ImportError
Traceback (most recent call last):
File "/var/task/handler.py", line 484, in lambda_handler
return LambdaHandler.lambda_handler(event, context)
File "/var/task/handler.py", line 240, in lambda_handler
handler = cls()
File "/var/task/handler.py", line 143, in __init__
wsgi_app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS)
File "/var/task/django_zappa_app.py", line 20, in get_django_wsgi
return get_wsgi_application()
File "/tmp/pip-build-dt_DVN/Django/django/core/wsgi.py", line 13, in get_wsgi_application
File "/tmp/pip-build-dt_DVN/Django/django/__init__.py", line 27, in setup
File "/tmp/pip-build-dt_DVN/Django/django/apps/registry.py", line 85, in populate
File "/tmp/pip-build-dt_DVN/Django/django/apps/config.py", line 94, in create
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named accounts
[1496566112758] 'NoneType' object is not callable
`
My project tree:
.
├── apps
│   ├── accounts
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── filters.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── serializers.py
│   │   ├── tests.py
│   │   └── views.py
│   ├── helpers
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   └── views.py
│   ├── __init__.py
│ 
├── manage.py
├── my_project
│   ├── apiv1_urls.py
│   ├── __init__.py
│   ├── partner_logging.py
│   ├── settings
│   │   ├── base.py
│   │   ├── conf.ini
│   │  
│   │   ├── dev.py
│   │   ├── __init__.py
│   │   └── prod.py
│   ├── urls.py
│   └── wsgi.py
├── requirements
│   ├── base.txt
│   └── dev.txt
└── zappa_settings.json
zappa_settings.json
{
"new2": {
"aws_region": "ap-south-1",
"django_settings": "my_project.settings",
"profile_name": "default",
"s3_bucket": "xyz"
}
}
wsgi.py
import os
import sys
from django.core.wsgi import get_wsgi_application
PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, os.path.join(PROJECT_ROOT, 'apps'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
application = get_wsgi_application()

My problem turned out to be that my application and my virtual environment have the same name. So if you're using virtualenvwrapper and your venv is called apps or accounts try making a new one with a different name.
zappa package $ENV -o zappa.zip && unzip -vl zappa.zip if you want to see what it's actually uploading
https://github.com/Miserlou/Zappa#package
$ zappa -v
0.46.1
$ zappa package -h
usage: zappa package [-h] [--all] [-a APP_FUNCTION] [-s SETTINGS_FILE] [-q]
[-j] [--disable_progress] [-o OUTPUT]
[stage_env]
positional arguments:
stage_env

Related

Django error with test module while running tests

When i run python manage.py tests i get an error saying that some test module is not found.
I am using PyCharm, Django 2.1.4 and W10 on Ubuntu.
The error:
======================================================================
ERROR: projectname.projectname (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: projectname.projectname
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/loader.py", line 462, in _find_test_path
package = self._get_module_from_name(name)
File "/usr/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
ModuleNotFoundError: No module named 'projectname.projectname'
What I've tried
python manage.py runserver and it runs just fine.
Add projectname to INSTALLED_APPS
Create and app called tests
My project structure
Django
│   ├── requirements.txt
│   └── projectname
│   ├── __init__.py
│   ├── manage.py
│   └── projectname
│   ├── apps
│   │   ├── accounts
│   │   │   ├── admin.py
│   │   │   ├── apps.py
│   │   │   ├── __init__.py
│   │   │   ├── migrations
│   │   │   │   ├── __init__.py
│   │   │   ├── models
│   │   │   │   ├── __init__.py
│   │   │   │   ├── profiles.py
│   │   │   │   └── users.py
│   │   │   ├── serializers
│   │   │   │   └── __init__.py
│   │   │   ├── tests.py
│   │   │   ├── urls.py
│   │   │   └── views
│   │   │   └── __init__.py
│   │   ├── __init__.py
│   ├── db.sqlite3
│   ├── __init__.py
│   ├── settings
│   │   ├── base.py
│   │   ├── development.py
│   │   ├── production.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
I just want to run my tests like in any other django project...
I have never encountered this problem before so any help is appreciated! :)
Well, well...it turns out that changing the folder was the solutio, though i have projects working that share the same folder name so i don't really know what happend with this one.
Before:
Django
│ └── projectname
│ └── projectname
After:
Django
│ └── othername
│ └── projectname

Gunicorn ImportError: No module name myApp

I am trying to deploy my code on Heroku but gunicorn is giving error ImportError: No module named inventory.
My Directory Structure
--server
|
|--server
├── __init__.py
├── home
│   ├── __init__.py
│   ├── admin.py
│   ├── migrations
│   │   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── views.py
├── inventory
│   ├── __init__.py
│   ├── admin.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   ├── models.py
│   ├── serializer.py
│   ├── tests.py
│   ├── views.py
├── manage.py
└── server
├── __init__.py
├── settings.py
├── urls.py
├── wsgi.py
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.server.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
Basically my main server Django project is in /server/server/settings.py
Need Help here
The DJANGO_SETTINGS_MODULE parameter should be set from the root of your project, that should be:
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"server.settings")

Running unit tests on the "Flaskr" tutorial micro-blogging app in Flask

I've cloned the flaskr application from Github and am trying to follow the Testing Flask Applications tutorial. Following Bonus: Testing the Application, I've added a subdirectory test to the top-level flaskr directory, so that my directory tree looks like this:
.
├── build
│   ├── bdist.linux-x86_64
│   └── lib.linux-x86_64-2.7
│   └── flaskr
│   ├── flaskr.py
│   ├── __init__.py
│   ├── schema.sql
│   ├── static
│   │   └── style.css
│   └── templates
│   ├── layout.html
│   ├── login.html
│   └── show_entries.html
├── dist
│   └── flaskr-0.0.0-py2.7.egg
├── flaskr
│   ├── flaskr.db
│   ├── flaskr.py
│   ├── flaskr.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── schema.sql
│   ├── static
│   │   └── style.css
│   └── templates
│   ├── layout.html
│   ├── login.html
│   └── show_entries.html
├── flaskr.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   └── top_level.txt
├── MANIFEST.in
├── README
├── setup.cfg
├── setup.py
├── test
│   └── test_flaskr.py
└── tests
└── test_flaskr.py
Note that there are also 'built-in' tests in the directory tests; however, I'm writing tests in test_flaskr.py in the directory tests. So far I'm trying just one test:
import os
import flaskr
import unittest
import tempfile
class FlaskrTestCase(unittest.TestCase):
def setUp(self):
self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
flaskr.app.config['TESTING'] = True
self.app = flaskr.app.test_client()
with flaskr.app.app_context():
flaskr.init_db()
def tearDown(self):
os.close(self.db_fd)
os.unlink(flaskr.app.config['DATABASE'])
def test_empty_db(self):
rv = self.app.get('/')
assert b'No entries here so far' in rv.data
if __name__ == '__main__':
unittest.main()
However, if I try to run this I get the following error:
Traceback (most recent call last):
File "/home/kurt/dev/scratch/flask/examples/flaskr/test/test_flaskr.py", line 13, in setUp
flaskr.init_db()
AttributeError: 'module' object has no attribute 'init_db'
I don't understand this error. My flaskr.py is the same as the one on https://github.com/pallets/flask/blob/master/examples/flaskr/flaskr/flaskr.py and has an init_db function defined. How can I make the unit test run?
When you import the flaskr package in your script, you can access the variables, functions etc. declared and defined in flaskr/__init__.py
init_db is not defined in flask/__init__.py, the code provided, expects init_db to be defined in flask/__init__.py.
There are two ways to fix the issue you have:
Replace flaskr.init_db() with flaskr.flaskr.init_db()
Modify the import statement in test_flaskr.py as follows:
Change import flaskr to from flaskr import flaskr

Issue with turning a test file into a package with __init__.py

I am following along with this tutorial:
http://www.marinamele.com/taskbuster-django-tutorial/create-home-page-with-tdd-staticfiles-templates-settings
When I run:
(tb_test)metersk:functional_tests metersky$ python test_all_users.py
.
----------------------------------------------------------------------
Ran 1 test in 2.313s
OK
The test works as it should. The test being :
from selenium import webdriver
import unittest
class NewVisitorTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
def test_it_worked(self):
self.browser.get('http://localhost:8000')
self.assertIn('Welcome to Django', self.browser.title)
if __name__ == '__main__':
unittest.main(warnings='ignore')
However, when I follow the step to add a blank __init__.py file to the directory, and I run the test like so: python manage.py test functional_tests
I get a the following failure:
Traceback (most recent call last):
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 20, in get_env_variable
return os.environ[var_name]
File "/Users/metersky/.virtualenvs/tb_test/bin/../lib/python3.4/os.py", line 633, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/__init__.py", line 2, in <module>
from .base import *
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 31, in <module>
SECRET_KEY = get_env_variable('SECRET_KEY')
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 23, in get_env_variable
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: set the SECRET_KEY environment variable
I don't quite understand how calling the test script differently from the command line would prevent the program from finding the secret key environment variable. It clearly works when I call the script on its own.
Folder structure:
├── TaskBuster.sublime-project
├── TaskBuster.sublime-workspace
├── db.sqlite3
├── functional_tests
│   ├── __init__.py
│   └── test_all_users.py
├── manage.py
├── requirements
│   ├── base.txt
│   ├── development.txt
│   ├── production.txt
│   └── testing.txt
└── taskbuster
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-34.pyc
│   ├── settings.cpython-34.pyc
│   ├── urls.cpython-34.pyc
│   └── wsgi.cpython-34.pyc
├── db.sqlite3
├── settings
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── base.cpython-34.pyc
│   │   └── development.cpython-34.pyc
│   ├── base.py
│   ├── development.py
│   ├── production.py
│   ├── staging.py
│   └── testing.py
├── static
│   ├── css
│   │   ├── bootstrap-theme.css
│   │   ├── bootstrap-theme.css.map
│   │   ├── bootstrap-theme.min.css
│   │   ├── bootstrap.css
│   │   ├── bootstrap.css.map
│   │   ├── bootstrap.min.css
│   │   └── main.css
│   ├── favicon.ico
│   ├── fonts
│   │   ├── glyphicons-halflings-regular.eot
│   │   ├── glyphicons-halflings-regular.svg
│   │   ├── glyphicons-halflings-regular.ttf
│   │   └── glyphicons-halflings-regular.woff
│   ├── img
│   └── js
│   ├── main.js
│   └── vendor
│   ├── bootstrap.js
│   ├── bootstrap.min.js
│   ├── jquery-1.11.2.min.js
│   ├── modernizr-2.8.3-respond-1.4.2.min.js
│   └── npm.js
├── templates
│   ├── 404.html
│   ├── base.html
│   ├── humans.txt
│   └── robots.txt
├── urls.py
└── wsgi.py

How to upload the django-1.5 application in google app engine?

I have uploaded the my sample CRUD application in google app engine. But it riased error as shown below,
Traceback (most recent call last):
File "/base/data/home/apps/s~mobiheropython/1.367549640437962894/main.py", line 52, in <module>
main()
File "/base/data/home/apps/s~mobiheropython/1.367549640437962894/main.py", line 49, in main
util.run_wsgi_app(application)
File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/webapp/util.py", line 98, in run_wsgi_app
run_bare_wsgi_app(add_wsgi_middleware(application))
File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/webapp/util.py", line 116, in run_bare_wsgi_app
result = application(env, _start_response)
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/core/handlers/wsgi.py", line 236, in __call__
self.load_middleware()
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/core/handlers/base.py", line 51, in load_middleware
mod = import_module(mw_module)
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/contrib/auth/middleware.py", line 3, in <module>
from django.contrib.auth.backends import RemoteUserBackend
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/contrib/auth/backends.py", line 3, in <module>
from django.contrib.auth.models import Permission
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/contrib/auth/models.py", line 8, in <module>
from django.db import models
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/db/__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/db/utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/db/utils.py", line 27, in load_backend
return import_module('.base', backend_name)
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/python27_runtime/python27_lib/versions/third_party/django-1.5/django/db/backends/mysql/base.py", line 17, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
My app structure is as shown below,
mobihero
├── app.yaml
├── app.yaml~
├── favicon.ico
├── index.yaml
├── main.py
├── main.py~
├── main.pyc
├── manage.py
├── mobitracking
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.py~
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── nohup.out
├── Procfile
├── requirements.txt
├── shipment
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── static
│   ├── css
│   │   ├── bootstrap.css
│   │   └── styles.css
│   └── jquery
│   ├── jquery-1.9.1.js
│   ├── jquery-ui-1.10.1.custom.min.js
│   ├── ui-darkness
│   │   ├── images
│   │   │   ├── animated-overlay.gif
│   │   │   ├── ui-bg_flat_30_cccccc_40x100.png
│   │   │   ├── ui-bg_flat_50_5c5c5c_40x100.png
│   │   │   ├── ui-bg_glass_20_555555_1x400.png
│   │   │   ├── ui-bg_glass_40_0078a3_1x400.png
│   │   │   ├── ui-bg_glass_40_ffc73d_1x400.png
│   │   │   ├── ui-bg_gloss-wave_25_333333_500x100.png
│   │   │   ├── ui-bg_highlight-soft_80_eeeeee_1x100.png
│   │   │   ├── ui-bg_inset-soft_25_000000_1x100.png
│   │   │   ├── ui-bg_inset-soft_30_f58400_1x100.png
│   │   │   ├── ui-icons_222222_256x240.png
│   │   │   ├── ui-icons_4b8e0b_256x240.png
│   │   │   ├── ui-icons_a83300_256x240.png
│   │   │   ├── ui-icons_cccccc_256x240.png
│   │   │   └── ui-icons_ffffff_256x240.png
│   │   ├── jquery-ui-1.10.1.custom.css
│   │   └── jquery-ui-1.10.1.custom.min.css
│   └── ui-lightness
│   ├── images
│   │   ├── animated-overlay.gif
│   │   ├── ui-bg_diagonals-thick_18_b81900_40x40.png
│   │   ├── ui-bg_diagonals-thick_20_666666_40x40.png
│   │   ├── ui-bg_flat_10_000000_40x100.png
│   │   ├── ui-bg_glass_100_f6f6f6_1x400.png
│   │   ├── ui-bg_glass_100_fdf5ce_1x400.png
│   │   ├── ui-bg_glass_65_ffffff_1x400.png
│   │   ├── ui-bg_gloss-wave_35_f6a828_500x100.png
│   │   ├── ui-bg_highlight-soft_100_eeeeee_1x100.png
│   │   ├── ui-bg_highlight-soft_75_ffe45c_1x100.png
│   │   ├── ui-icons_222222_256x240.png
│   │   ├── ui-icons_228ef1_256x240.png
│   │   ├── ui-icons_ef8c08_256x240.png
│   │   ├── ui-icons_ffd27a_256x240.png
│   │   └── ui-icons_ffffff_256x240.png
│   ├── jquery-ui-1.10.2.custom.css
│   └── jquery-ui-1.10.2.custom.min.css
└── templates
├── add_shipment.html
├── all_shipments.html
├── base.html
├── home.html
├── search.html
├── shipment
│   └── shipment_confirm_delete.html
├── shipment_detail.html
└── update_shipment.html
I have uploaded code using the command as shown below,
appcfg.py update mobihero
It is successfully deployed.
Please solve my problem. Thanks.....
Do you change your Mysql backend?
Support for using Google Cloud SQL with Django is provided by a custom Django database backend which wraps Django's MySQL backend.
edit your settings.py:
DATABASES = {
'default': {
'ENGINE': 'google.appengine.ext.django.backends.rdbms',
'INSTANCE': 'my_project:instance1',
'NAME': 'my_database',
}
}

Categories