DEV Community

Discussion on: How to deploy a Python/Flask App to Vercel

Collapse
 
maxdevjs profile image
maxdevjs

Hello, thank you for this tutorial.

Locally, if I do an export FLASK_APP=index.py I get a 127.0.0.1 - - [18/Sep/2020 08:24:15] "GET / HTTP/1.1" 500 terminal message and a flask.cli.NoAppException: Could not import "index". browser message.

Works correctly for routes with an export FLASK_APP=/absolute/path/to/index.py, but again I get a FileNotFoundError: [Errno 2] No such file or directory: './data.json'. If I use an absolute path also for the data.json file, it works (locally, but, obviously, breaks the api route when deployed).

flask --version
Python 3.7.7
Flask 1.1.2
Werkzeug 1.0.1
Enter fullscreen mode Exit fullscreen mode

Your version works perfectly when deployed to Vercel.

Any idea about those local paths?

P.S.: Vercel build log reports Warning: Due tobuildsexisting in your configuration file, the Build and Development Settings defined in your Project Settings will not apply. Is that right?

Collapse
 
andrewbaisden profile image
Andrew Baisden • Edited

Hi what operating system and code editor are you using? Check out the official docs here they might help flask.palletsprojects.com/en/1.1.x...

So about the build logs I just did a test. If you remove this code below from the vercel.json file then that log goes away however the routing will not work so it is needed. It's probably just a note and not a serious warning. And you can use the command vercel --prod for production deployments that will get rid of the production log note too I updated the guide to reflect this.

    "builds": [
        {
            "src": "./index.py",
            "use": "@vercel/python"
        }
    ],
Collapse
 
maxdevjs profile image
maxdevjs • Edited

Thank for the reply.

I use nvim on Solus.

I did not find anything really useful in the documentation about this case, probably because it is the first time I meet Flask :)

As a temporary solution I got this:

import os

with open(
            os.path.join(
            os.path.dirname(
            os.path.abspath(__file__)),
            'data.json')) as file:

I guess is not an optimal solution, but things like this are the only way that I found so far to make it work (don't mind the indentation). Quickly tested and works on Vercel too.

It seems related to how open behaves in Flask (perhaps the version I do have?). I am really curious about what can be the cause.

Thread Thread
 
andrewbaisden profile image
Andrew Baisden

Ah ok that explains it. So Solus is a version of Linux? The Flask documentation only mentions Mac and Windows in the documentation as far as I can see. However you seem to have figured out how to get it working. I am sure you will figure it out as you have experience working with Solus which I have never used before.

Thread Thread
 
maxdevjs profile image
maxdevjs

Yes, it is a Linux flavor. Thank you again for the tutorial :)