Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
Launch Flask app factory (with create_app()) from external file

Following this tutorial to dockerize a Flask stack, I am trying to lauch an existing application from an external manage.py file

My app structure goes like:

└── services
    └── web
        ├── manage.py
        └── myapp
            └── requirements.txt
            └── myserver
                └──__init__.py
                └──config
                   └──config.py

The manage.py file:

import os
os.chdir('myapp')
from flask.cli import FlaskGroup
from myapp.myserver import create_app

app = create_app()

cli = FlaskGroup(app)


if __name__ == "__main__":
    cli()

and finally, the myapp/myserver/__init__.py file (simplified):

from myserver.config.config import Config # this file exists and imports work when i launch from pycharm 

def create_app():
    app = Flask(__name__)
    ...other stuff here
    return app

So when I try to run: python3 manage.py run, the ouput goes:

  File "manage.py", line 4, in <module>
    from myapp.myserver import create_app
  File "/var/www/html/flask-docker/services/web/myapp/myserver/__init__.py", line 12, in <module>
    from myserver.config.config import Config

The config import from __init__.py cannot be resolved. I tried to solve this with the chdir visible in manage.py.

My env variables are the one I got from a working Pycharm flask setup :

FLASK_APP="myserver:create_app()"

I managed to sucessfully pass the import instruction by replacing :

from myserver.config.config import Config

by

from .config.config import Config

but the whole project contains imports beginning by from myserver

So the final question is: why the init file inside myserver does not recognize myserver folder/module ? This setup works just fine when I laucnh it via PyCharm

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

It can't find the module because it doesn't know what directory to search to find the file manage.py.

try to add manage.py information to init file. from web import manage

over 4 years ago · Santiago Trujillo Report

0

Brandons comment and this answer led me to a solution

manage.py

import sys
#this is the fix
sys.path.append(os.path.join(os.path.dirname(__file__),'myapp'))
from flask.cli import FlaskGroup
from myapp.myserver import create_app

app = create_app()

cli = FlaskGroup(app)
## Heading ##
if __name__ == "__main__":
    cli()

Instead of changing the working directory with os.chdir, I change it using sys.path. I am now able to use my servers init file

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!