I am trying to study how to use alembic in flask, I want to import a method in flask app:
tree .
.
├── README.md
├── alembic
│ ├── README
│ ├── env.py
│ ├── env.pyc
│ ├── script.py.mako
│ └── versions
│ ├── 8f167daabe6_create_account_table.py
│ └── 8f167daabe6_create_account_table.pyc
├── alembic.ini
├── app
│ ├── __init__.py
│ ├── main
│ │ ├── __init__.py
│ │ ├── errors.py
│ │ ├── forms.py
│ │ └── views.py
│ ├── models.py
│ └── templates
│ ├── 404.html
│ ├── 500.html
│ ├── base.html
│ ├── index.html
│ └── user.html
├── config.py
├── data.sqlite
├── manage.py
└── requirements.txt
in app/__init__.py:
def create_app(config_name):
app = Flask(__name__)
I want to import create_app in env.py:
from app import create_app
but the error shows as below when I run the command alembic upgrade head:
File "alembic/env.py", line 5, in <module>
from app import create_app
ImportError: No module named app
Any idea for this?
I guess you are trying to run
python env.py
In this case, your app directory is not in PYTHONPATH.
solution 1
Run the app from parent dir:
python alembic/env.py
solution 2
Set the PYTHONPATH before running the app
PYTHONPATH=/path/to/parent/dir python env.py
edit
I read about alembic. As #mrorno said, just set the PYTHONPATH before running alembic:
PYTHONPATH=. alembic upgrade head
alembic just tries to load your env.py source code. It's not in your package, so it can't access your app module.
Use solution 2 #tomasz-jakub-rup suggested, you can execute like
$ PYTHONPATH=. alembic upgrade head
and you should get your result.
Create file .env and insert PYTHONPATH=.
Related
Pycharm imports works correctly, but when I execute test_settings.py in the terminal, I have this error:
ImportError while importing test module '/home/ed/PycharmProjects/TTime/tests/test_settings.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_settings.py:3: in
from ttime import app, db
E ModuleNotFoundError: No module named 'ttime'
Here is my Flask project structure. I tried to include init.py in the main folder and in the tests folder, but nothing changes. It seems like somethings is wrong with python paths.
.
├── config.py
├── README.md
├── requirements.txt
├── runserver.py
├── tests
│ ├── functional
│ │ └── __init__.py
│ ├── pytest.ini
│ ├── test_settings.py
│ └── unit
│ ├── __init__.py
│ └── test_models.py
└── ttime
├── __init__.py
├── models.py
├── routes.py
├── static
└── templates
├── base.html
└── index.html
I have a Flask app that I am trying to put on heroku. I have a requirements.txt file with the requirements for my project, and heroku says that this should be enough to let heroku detect python but it does not. I can manually set the buildpack to python like so
heroku buildpacks:set heroku/python but then I get this error: (from running git push heroku master)
remote: -----> App not compatible with buildpack: https://buildpack-registry.s3.amazonaws.com/buildpacks/heroku/python.tgz
remote: More info: https://devcenter.heroku.com/articles/buildpacks#detection-failure
No default language could be detected for this app.`
What is wrong with my project layout?
Here is my file tree:
.
├── faceParser
│ ├── __init__.py
│ ├── recolor.py
│ ├── static
│ │ ├── libs
│ │ │ ├── bootstrap.min.css
│ │ │ ├── bootstrap.min.js
│ │ │ ├── jquery.min.js
│ │ │ ├── notify.js
│ │ │ └── webcam.min.js
│ │ ├── sketch.js
│ │ └── style.css
│ └── templates
│ ├── base.html
│ └── index.html
├── main.sh
├── README.md
├── requirements.txt
└── venv
(virtual environment files omitted because there are a lot of them)
This is how I run it locally:
export FLASK_APP=faceParser
export FLASK_ENV=development
flask run
Thanks!
I changed my project layout to contain an app.py file in the outermost directory, and added a Procfile file and a runtime.txt file (python-3.5.2)
I think that Heroku needs these files to understand that it is a python project.
Now it works.
According to this, I am trying to run my flask web application from my com_profiler directory as python -m api.index, python -m api.index.py, python api/index.py. But none of these are working. The errors I am getting are in the order -
-> ValueError: attempted relative import beyond top-level package
-> attempted relative import beyond top-level package
-> SystemError: Parent module '' not loaded, cannot perform relative import
Directory Structure:
comp_profiler/
├── api
│ ├── bootstrap.sh
│ ├── index.py
│ └── __init__.py
├── __init__.py
├── pipelines.py
├── random_useragent.py
├── requirements.txt
├── scrapy.cfg
├── spiders
│ ├── __init__.py
│ ├── content_handler.py
│ ├── core_spider.py
│ ├── middlewares.py
│ ├── scrapper
│ │ ├── __init__.py
│ │ ├── corporatedir.py
│ │ ├── craft.py
│ │ ├── tofler.py
│ │ └── zaubacorp.py
│ ├── scrapper.py
│ ├── seed_list_generator.py
│ ├── settings.py
│ └── utility.py
index.py
from flask import Flask, request, jsonify
from ..spiders.seed_list_generator import SeedListGenerator
app = Flask(__name__)
#app.route("/start-spider")
def hello_world():
spider = SeedListGenerator()
company_name = request.get_json()
print(company_name)
return "Hello world"
if __name__ == "__main__":
app.run()
I also tried running it from api directory, but no success.
Kindly let me know if I am missing something with flask setup, as I am just starting up with flask.
Update: I want to integrate SeedListGenerator as API call. Kindly suggest.
Thanks in advance.
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
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.