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")
Related
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
Django version 1.9.7.
My current project structure is:
vehicles/
├── etl
│ ├── etl
│ ├── manage.py
│ ├── pipeline
│ └── bku
└── web
├── db.sqlite3
├── manage.py
├── profiles
├── projects
├── reverse
├── static
├── templates
├── bku
│ ├── admin.py
│ ├── admin.pyc
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── static
│ ├── templates
│ ├── tests.py
│ ├── urls.py
│ ├── views.py
│ └── views.pyc
└── rocket
├── celery.py
├── __init__.py
├── settings
│ ├── base.py
│ ├── dev.py
│ ├── __init__.py
│ ├── local.py
│ ├── production.py
│ ├── test.py
├── urls.py
├── wsgi.py
Now I want to use Celery in the bku Django app. But when I run the worker celery -A rocket worker -l info I get the following error django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.. I have the SECRET_KEY defined and I didn't have this error before trying Celery.
How can I run the worker?
rocket/celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rocket.settings')
app = Celery('rocket')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
#app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
rocket/init.py
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ['bku']
The error message is a bit misleading—usually when you see an ImproperlyConfigured exception like that it means that Django can't find your settings file.
In your case you're setting the DJANGO_SETTINGS_MODULE environment variable to rocket.settings, but from your directory structure it looks like it should instead be something like rocket.settings.production.
I am working through Django 1.9 Django 1.9 Tutorial Part 5.
I am using Python 2.7.6 and Django 1.9.4.
The tree structure of my folders is:
django-mysite/
├── db.sqlite3
├── manage.py
├── mysite
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── __pycache__
│ │ ├── __init__.cpython-34.pyc
│ │ ├── __init__.cpython-34.sublime-workspace
│ │ ├── settings.cpython-34.pyc
│ │ ├── urls.cpython-34.pyc
│ │ └── wsgi.cpython-34.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
└── polls
├── admin.py
├── admin.pyc
├── apps.py
├── apps.pyc
├── __init__.py
├── __init__.pyc
├── migrations
│ ├── 0001_initial.py
│ ├── 0001_initial.pyc
│ ├── __init__.py
│ └── __init__.pyc
├── models.py
├── models.pyc
├── templates
│ └── polls
│ ├── detail.html
│ ├── index.html
│ ├── results.html
│ └── tests.py
├── tests.py
├── tests.pyc
├── urls.py
├── urls.pyc
├── views.py
└── views.pyc
When I run tests through command:
python manage.py test polls
or
python manage.py test polls.tests
It does not run tests. The output is:
Creating test database for alias 'default'...
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
The tests.py file has the code (as in tutorial)
import datetime
from django.utils import timezone
from django.test import TestCase
from .models import Question
class QuestionMethodTests(TestCase):
def test_was_published_recently_with_future_question(self):
"""
was_published_recently() should return False for questions whose
pub_date is in the future.
"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertEqual(future_question.was_published_recently(), False)
Whats wrong?
Remove the tests.py from template/polls folder and then try.
To run the test, try python manage.py test polls
Following microblog tutorial on Flask: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
In Pycharm, no matter how I structure or name the files, I cannot get the dev server to run if I separate the code and import the files. I also can't get it to inherit the codes no matter where I move the init, views, run files. The only way for me to get the server to run is to have all the commands execute on the same file. What am I doing wrong?
I have it setup as:
Project 1 > app(directory) > tmp(directory) > run.py(file)
app(directory) > static(directory) > templates(directory) > init.py(file) > views.py(file) (I have tried different arrangements.)
Inside views.py:
from app import app
Inside run.py:
from app import app
Inside init.py:
from flask import Flask
from app import views
(I have tried many different combinations such as from app import app.views. from app import views as app_views. I have also tried renaming the directories/files, nothing is working.)
Build the new project with PyCharm, it will create a virtual environment for you. Then put these into a run.py in a root of your
a project like that (don't forget to turn debugging mode off in prod)
from app import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)
Set up init.py file inside you 'app':
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
bcrypt.init_app(app)
login_manager.init_app(app)
mail.init_app(app)
Store your credentials into Config class:
class Config:
SECRET_KEY = 'your key... '
SQLALCHEMY_DATABASE_URI = 'your db...'
SQLALCHEMY_TRACK_MODIFICATIONS = False
MAIL_SERVER = 'smtp.google.com'
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = 'your email'
MAIL_PASSWORD = 'email password'
Structure your project, placing an empty init.py into each directory( accordingly to your architecture). Here is an example below, how to structure your project in Flask. It runs with no problem on
.
├── README.md
├── app
│ ├── __init__.py
│ ├── config.py
│ ├── errors
│ │ ├── 403.html
│ │ ├── 404.html
│ │ ├── 500.html
│ │ ├── __init__.py
│ │ └── handlers.py
│ ├── main
│ │ ├── __init__.py
│ │ └── routes.py
│ ├── models.py
│ ├── posts
│ │ ├── __init__.py
│ │ ├── forms.py
│ │ └── routes.py
│ ├── site.db
│ ├── static
│ │ ├── main.css
│ │ └── profile_pics
│ │ ├── 3c4feb2bb50d90df.png
│ │ ├── ba3d328163a8125e.png
│ │ └── default.jpg
│ ├── templates
│ │ ├── about.html
│ │ ├── account.html
│ │ ├── create_post.html
│ │ ├── home.html
│ │ ├── layout.html
│ │ ├── login.html
│ │ ├── post.html
│ │ ├── register.html
│ │ ├── reset_request.html
│ │ ├── reset_token.html
│ │ └── user_posts.html
│ └── users
│ ├── __init__.py
│ ├── forms.py
│ ├── routes.py
│ └── utils.py
└── run.py
I have the following directory tree and I'm trying to import models in my_spider.py:
.
├── my_spider
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── items.py
│ ├── pipelines.py
│ ├── settings.py
│ ├── settings.pyc
│ └── spiders
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── my_spider.py
│ ├── my_spider.pyc
│ └── models.py
└── scrapy.cfg
In my_spider.py, I have:
from my_spider.spiders import models
I get an error saying ImportError: No module named spiders. I must be doing something very basic wrong.
Any help will be appreciated.
Thanks
Since my_spider.py and models.py are in the same directory, I think you should just use import models