-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Routing problems with Python #3729
Comments
Hi @Uninen It sounds like you are trying to create one python function that responds to multiple routes. This is achievable but is an anti-pattern. Routing in ZEIT Now is handled by the file system, so If you want to rewrite multiple routes to a single function, you can configure rewrites in |
I did try rewrites also, but couldn't get them working (404s or 502s as well). But if the file-based routing is the preferred method, I can of course just copy paste the boilerplate into multiple files. |
You shouldn’t need to copy/paste because python can import shared files. You can put any common code outside the api directory or put it inside with an underscore, for example |
Previously, python would fail when using [Path Segments](https://zeit.co/docs/v2/serverless-functions/introduction#path-segments) as seen in #3729. This PR changes the import logic so that the entrypoint file is [imported as a relative file path](https://stackoverflow.com/a/67692/266535) instead of by module name.
@styfle I tried this method but it doesn't seem to work. I have My from flask import Flask, Response
from _utils.common import VAR
app = Flask(__name__)
print(VAR)
... When I try to visit any API route, I get this error:
Everything works fine when I run the code with Is there an official example I can refer to? |
I've tested just about everything but can't get any other routing working on Python except
/api
.I'm trying to set up Starlette (or Sanic, or whatever, doesn't really matter) to respond from one app (file) to several URLs. (For example.
/api/foo
and/api/bar
that would be routed in different handler functions in the single app with a router.)Full source code of my test app:
(This works ok with uvicorn or other servers.)
When I save this as
api/index.py
the/api
url works, but no other URL. When I save this asapi/[index].py
orapi/[anything].py
I get a 502 + following error in the logs:Syntax error in module 'now__handler__python': invalid syntax (now__handler__python.py, line 7)
All other setup combinations result in 404 page (from ZEIT, not from the app) and no logs whatsoever.
Is it even possible to do basic URL routing with python? (Also there are zero examples or documentation on how to do this, that would probably help as well.)
The text was updated successfully, but these errors were encountered: