I have 2 files in a folder named "pyproj"
main.py
post.py
folder path image
whenever I run the localhost with uvicorn, main.py is only shown in the local host browser.
In post.py I have the app instance as "app", so I run the uvicorn in terminal as
uvicorn main:app --reload
In post.py I have the app instance as "app2", so I run the uvicorn in terminal as
uvicorn post:app2 --reload
no matter what code I run from the above I can see only the main.py's paths. Can someone help with this.
Related
I am trying to deploy my dash app which uses dash_extensions, Dash_proxy and has multiple pages in the pages folder on GCP cloud run using gunicorn but the app cannot find the pages folder. It works perfectly fine when I use the development server but breaks in the production server because it cannot find the folder path.
The app (following code is inside the app.py file):
app = DashProxy(use_pages=True, pages_folder=pages_folder, external_stylesheets=[dbc.themes.SIMPLEX])
The app.py file and the pages folder are in the same directory
I have tried tried to following methods to get the folder path:
pages_folder="pages"
pages_folder=os.path.join(os.path.dirname(__file__), "pages")
for p in Path('.').rglob('*'):
if str(p).endswith('pages'):
pages_folder = str(p)
break
None of the above three work in when deploying on gcp using gunicorn through docker:
Dockerfile command:
CMD ["gunicorn" , "-b", "0.0.0.0:8000", "app:server"]
But if I use dev server through docker like following code it works:
CMD python app.py
Does anyone have any ideas of how to make it work with gunicorn?
Thanks for the help!
-Rexon
Yes I did. Just had to specify the root folder. This is what I did and it seems to work for me.
pages_folder=os.path.join(os.path.dirname(__name__), "pages")
app = DashProxy(__name__,use_pages=True, pages_folder=pages_folder, external_stylesheets=[dbc.themes.SIMPLEX])
server=app.server
I have built a package called mypkg that starts a guincorn server. This gunicorn server needs a config.py file, so I have stored this file in the package itself.
I use a separate script to start server. How can I reference this config.py file from script.
# My package installed at /home/anaconda3/envs/test-bot/lib/python3.8/site-packages/mypkg/
# it's content is
server.py config.py
When I start the `gunicorn server I use following command
gunicorn 'mypkg.server:create_app()' --worker-class=gevent -w 1 --bind 0.0.0.0:5006 --timeout 30 --config mypkg.config
Now because mypkg is installed in environment when using gunicorn mypkg.server works but I can't reference mypkg.config.py. Can someone suggest me a way to do that.
I am using FastAPI and it works fine. But I want to use python some.py to do something extra.
In that some.py I have imported app.core.config. But when I use python some.py in the project env, it always give me the error.
from app.core.config import settings
if __name__ == '__main__':
print('hello')
I tried with any other file and it gives the same error.
I have already included the __init__.py file in the app and core directories. It works fine when using uvicorn to start the fastapi service.
My question is, how to run a single py file (with fastapi app module imported) manually?
Try by renaming the directory app to apps or something else and then try again.
I had this same problem months ago, so you can understand with this example. And in all my research, I decided to make the structure like this to run:
#in docker
CMD ["python", "-m","uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8050"]
#in bash
python -m uvicorn app.main:app --host 0.0.0.0 --port 8050
#In app/main.py
def create_app():
"""Create the app"""
_app = FastAPI(
title="Project",
)
#_app.get("/")
def read_root():
"""Root route"""
return {"Ping": "Pong"}
return _app
app = create_app()
This bash command will execute your instance in main.py (he tries to pick up the var name in this example ":app")
I am following the instructions here to deploy an app in Google App Engine. Everything works correctly.
Nevertheless, Google, by default, looks for the main folder (where app = Flask(__name__) is defined) in main.py. How could I redefine this? I would like to define this main folder as app.py.
Rename main.py to app.py
Add entrypoint: gunicorn -b :$PORT app:app to your app.yaml file. This is where you are telling Google to find the app object in a file called app
Add gunicorn to your requirements.txt file
Notes:
i. Because you're changing from main.py to app.py, you need to specify an entrypoint. GAE documentation says
If your app meets the following requirements, App Engine will start
your app with the gunicorn web server if you don't specify the
entrypoint field:
The root of your app directory contains a main.py file with a WSGI-compatible object called app.
Your app does not contain Pipfile or Pipfile.lock files.
ii. If you add an entrypoint, then you need to include gunicorn in your requirements.txt file
iii. I just tested the above configuration (the answer I gave) on a dev environment (Python 3.9 environment on Macbook using dev_appserver.py) and it works
I have a Flask app I am trying to serve via Gunicorn.
I am using virtualenv and python3. If I activate my venv cd to my app base dir then run:
gunicorn mysite:app
I get:
Starting gunicorn
Listening at http://127.0.0.1:8000
DEBUG:mysite.settings:>>Config()
...
Failed to find application: 'mysite'
Worker exiting
Shutting down: master
Reason: App failed to load
Looking in /etc/nginx/sites-available I only have the file 'default'. In sites-enabled I have no file.
In my nginx.conf file I have:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
App structure:
mysite #this is where I cd to and run gunicorn mysite:app
--manage.py
--/mysite
----settings.py
----__init__.py
in manage.py for mysite I have following:
logger.debug("manage.py entry point")
app = create_app(app_name)
manager = Manager(app)
if __name__ == "__main__":
manager.run()
In __init__.py file:
def create_app(object_name):
app = Flask(__name__)
#more setup here
return app
In my settings.py in the app directory
class Config(object):
logger.debug(">>Config()") #this logs OK so gunicorn is at least starting in correct directory
From inside the virtualenv if I run
print(sys.path)
I find a path to python and site-packages for this virtualenv.
From what I have read to start gunicorn it's just a matter of installing it and running gunicorn mysite:app
Running gunicorn from the parent directory of mysite I get the same failed to find application: 'mysite', App failed to load error, but don't get the DEBUG...Config() logged (as we are clearly in the wrong directory to start in). Running gunicorn from mysite/mysite (clearly wrong) I get and Exception in worker process ereor, ImportError: No module named 'mysite'.
Any clues as to how I can get gunicorn running?
You're pointing gunicorn at mysite:app, which is equivalent to from mysite import app. However, there is no app object in the top (__init__.py) level import of mysite. Tell gunicorn to call the factory.
gunicorn "mysite:create_app()"
You can pass arguments to the call as well.
gunicorn "mysite:create_app('production')"
Internally, this is equivalent to:
from mysite import create_app
app = create_app('production')
Alternatively, you can use a separate file that does the setup. In your case, you already initialized an app in manage.py.
gunicorn manage:app