I'm creating a Flask RESTful API. Here's the project structure:
│ .env
│ .flaskenv
│ .gitignore
│ <App Name>.code-workspace
│ <App Name>.py
│ dev_start
│ LICENSE
│ Pipfile
│ Pipfile.lock
│ README.md
│
├───<App Name>_api
│ errors.py
│ settings.py
│ __init__.py
│
├───crypto
│ password.py
│ __init__.py
│
├───models
│ blacklisted_token.py
│ company.py
│ user.py
│ __init__.py
│
├───resources
│ company.py
│ messages.py
│ user.py
│ __init__.py
│
└───schemas
company.py
user.py
__init__.py
Here's the .flaskenv file:
FLASK_APP=<App Name>:create_app()
FLASK_ENV=development
FLASK_DEBUG=1
FLASK_RUN_PORT=5000
FLASK_RUN_HOST=127.0.0.1
The "App Name" folder's "init.py" file has the "create_app" function defined. When I try to do a "pipenv run flask run" I get the following error:
Loading .env environment variables...
* Serving Flask app '<App Name>' (lazy loading)
* Environment: development
* Debug mode: on
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.
Error: While importing '<App Name>', an ImportError was raised.
What's really confusing is that there isn't an explanation of the ImportError.
Thanks in advance for any help. I've been searching for hours trying to find a solution to this.
In your .ENV:
FLASK_APP=<App Name>:create_app()
Try to replace with:
FLASK_APP=<App Name>:create_app
The possible reason is when you load the module you need to point on app variable or callable that return app.
Related
tree
├───pzApi
│ │ asgi.py
│ │ db.sqlite3
│ │ manage.py
│ │ settings.py
│ │ urls.py
│ │ wsgi.py
│ │ __init__.py
│ │
│ └───api
│ │ apps.py
│ │ funcs.py
│ │ urls.py
│ │ views.py
│ │ __init__.py
Procfile
release: python pzApi/manage.py migrate
web: gunicorn --bind 127.0.0.1:8000 pzApi.wsgi:application
I'm still trying to deploy this web app and after fixing the wsgi.py not found error by putting it in the same dir as manage.py, now I have another problem where the 'api' folder is not being found, any help is appreciated!
I found a similar question here but the solution did not work for me.
I have written a Flask app by following the official tutorial. I am trying to deploy it with Google App Engine. I have used Blueprints. The directory structure of my app looks like this:
app
│ .gcloudignore
│ app.yaml
│ main.py
│ requirements.txt
│ __init__.py
│
├───googleutils
│ │ utils.py
│
├───home
│ │ home.py
│ │
│ ├───static
│ ├───templates
│ │ home.html
│
├───models
│ │ Message.py
│ │ User.py
│
├───profile
│ │ profile.py
│ │
│ ├───static
│ ├───templates
│ │ edit_message.html
│ │ profile.html
│
├───sessions
│ │ sessions.py
│ │
│ ├───templates
│ │ login.html
│ │ logout.html
│ │ register.html
│
├───static
│ style.css
│
├───templates
│ layout.html
│ wrapper.html
In my app directory I have my app.yaml and requirements.txt. I used pip freeze in my venv and just chucked everything it spat out into requirements.txt.
The current Flask tutorial instructs you to start your app via command line using flask run. Hence, my application starts from __init__.py:
from flask import Flask
def create_app():
app = Flask(__name__, instance_relative_config=True)
# Application imports
from .home import home
from .sessions import sessions
from .profile import profile
# Blueprints
app.register_blueprint(home.home_bp)
app.register_blueprint(sessions.sessions_bp)
app.register_blueprint(profile.profile_bp)
return app
However, as noted in the question I linked above, gcloud looks for a main.py, so I added this to my root:
from app import create_app
app = create_app()
At the moment I'm just trying to get my index loaded, which is defined in home.py:
#home_bp.route('/', methods=['GET', 'POST'])
def home():
...
Here's my app.yaml:
runtime: python39
entrypoint: gunicorn "app:create_app()"
handlers:
- url: /static
static_dir: static
- url: /.*
script: auto
Truth be told I have no idea how to write this file, especially for directories with variables, such as this one defined in profile.py:
#profile_bp.route('/profile/<string:user_id>', methods=['GET', 'POST'])
def profile(user_id):
...
When I run gcloud app deploy, I get a 500 Server Error when I view my site in-browser, "The server encountered an error and could not complete your request.".
What am I doing wrong?
Well, I figured it out. My directory structure was wrong. It SHOULD be:
│ app.yaml
│ main.py
│ requirements.txt
├───app
│ │ __init__.py
│ └
Where app.yaml contains only:
runtime: python39
handlers:
- url: /.*
script: auto
Which causes GCloud to automatically look for the main.py file containing:
from app import create_app
app = create_app()
The whole webapp works this way.
I am following a tutorial on FLASK. Initially the routes were defined in a root file called "flaskblog.py". I had to set the FLASK_APP variable to "flaskblog.py" in order for "flask run" to work.
Later in the tutorial, the project was restructured into packages, and the "flaskblog.py" file renamed to "run.py". A folder (i.e. package) was created called "flaskblog".
To my surprise, "flask run" still worked, and even seems to flag it is using "flaskblog.py"...but this doesn't exist any more.
Folder structure:
(base) C:\flask_blog>tree /F
Folder PATH listing for volume OSDisk
Volume serial number is 36E9-84F4
C:.
│ run.py
│ site.db
│
├───.vscode
│ launch.json
│
├───flaskblog
│ │ forms.py
│ │ models.py
│ │ routes.py
│ │ site.db
│ │ __init__.py
│ │
│ ├───static
│ │ main.css
│ │
│ ├───templates
│ │ about.html
│ │ css.html
│ │ home.html
│ │ layout.html
│ │ layout_plain.html
│ │ login.html
│ │ register.html
│ │
│ └───__pycache__
│ forms.cpython-37.pyc
│ models.cpython-37.pyc
│ routes.cpython-37.pyc
│ __init__.cpython-37.pyc
│
└───__pycache__
app.cpython-37.pyc
flaskblog.cpython-37.pyc
forms.cpython-37.pyc
models.cpython-37.pyc
Note the absence of "flaskblog.py".
See what I get when I use "flask run":
(ariel) C:\flask_blog>SET FLASK_APP=flaskblog.py
(ariel) C:\flask_blog>flask run
* Serving Flask app "flaskblog.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
C:\ProgramData\Anaconda3\envs\ariel\lib\site-packages\flask_sqlalchemy\__init__.py:835: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
run.py is simply:
from flaskblog import app
if __name__ == '__main__':
app.run(debug=True)
effectively this only gets called if I want to start the app using "python run.py", and not "flask run".
You set the Flask App name as flaskblog.py using the command
SET FLASK_APP=flaskblog.py
If you reset it, it will have new name
I got the error:
File ".\main.py", line 3, in <module>
from app.core import config
ModuleNotFoundError: No module named 'app'
in the line 3 of main.py:
from app.core import config
I have tried the left empty the file app/init.py and with the next:
from .core import config
My structure folder is:
backend
│
└───app
│ .env
│ main.py
│ __init__.py
│
├───api
│ __init__.py
│
├───core
│ config.py
│ __init__.py
│
├───db
│ base.py
│ base_class.py
│ __init__.py
│
├───db_models
│ association_tables.py
│ team.py
│ user.py
│ __init__.py
│
└───schema
│ mutation.py
│ query.py
│ __init__.py
│
├───team
│ mutations.py
│ queries.py
│ types.py
│ __init__.py
│
└───user
mutations.py
queries.py
types.py
__init__.py
I'd due to problem with absolute/relative reference. I use PyCharm and I don't have alarm about the reachable or not of the modules.
thanks for advance
I rebuilt your structure and this should do the trick:
from core import config
I have a python Celery project of the following structure:
├── celeryconfig.py
├── Dockerfile
│ └── _templates
├── framework
│ ├── celery
│ │ ├── celery.py
│ │ ├── __init__.py
│ │ └── __init__.cpython-36.pyc
│ ├── tasks
│ │ ├── tasks.py
│ │ ├── __init__.py
│ ├── utilities
│ │ ├── utilities.py
│ │ ├── __init__.py
│ ├── tests
│ │ ├── __init__.py
│ │ └── test_celery.py
Which I run from the top level directory using the command celery framework.celery.celery worker
I have tets in the tests directory that I run using nose2. When I run it in the directory the tests pass without issue. However when the tests are run as part of a docker build process, the nose2 process fails because it can't make sense of the imports, for example
# test_celery.py
from framework.tasks.tasks import my_function
def test_my_function():
# Do some testing.
The import of the function succeeds, but fails on the imports of the imported file, which look like:
# tasks.py
from framework.utilities.utilities import some_other_function
Part of the issue I'm having is that Celery is particular about how it itself is structured, so attempts to restructure the directory have just resulted in celery not being able to start. I'm not quite sure why the tests only fail in a docker build, which looks like this:
FROM google/cloud-sdk:198.0.0-alpline
COPY . /project
WORKDIR /project
RUN apk add --no-cache python3 python3-dev && pip3 install -r requirements.txt
RUN nose2