Reputation: 309
I've been looking into flask lately and when looking at the tutorial information on the flask website, they have you install the demo webapp as a python package. I've seen this in some other demo flask webapps as well, but I haven't seen a reason why you would do this. The dev server renders the app without this and I would assume that a production server would once it is set up correctly.
This is probably an issue of simply not having the right mindset, but I just can't find a reason why I want to do this. Is it for easy replication or something else altogether?
Upvotes: 3
Views: 2188
Reputation: 78
Installing lets you test your app easier. Also check out Flask docs article about this. It says:
Installing also comes with other benefits that might not be obvious from the tutorial or as a new Python user, including:
- Currently, Python and Flask understand how to use the flaskr package only because you’re running from your project’s directory. Installing means you can import it no matter where you run from.
- You can manage your project’s dependencies just like other packages do, so pip install yourproject.whl installs them.
- Test tools can isolate your test environment from your development environment.
Upvotes: 1
Reputation: 127330
It makes reasoning about your program in different environments simpler, although that may be hard to see if you haven't experienced issues related to it.
The main reason is dealing with the Python path. If you don't install your application, then you are relying on Python implicitly adding the current directory when you run your program. Typically, you run in development from the project root, so everything works out. But in production, the WSGI server will be running from somewhere else (a system service, for example), so you have to jump through hoops to set up the Python path, otherwise you get import errors. If you installed the package, it would be on the path no matter what ran it.
Additionally, installing your application means using a virtual environment and keeping your dependencies isolated, rather than installing to the system Python.
Ultimately, it's encouraged because it makes you think about how to make your application re-usable, rather than dependent on the structure and location of your repository.
The deploy story becomes:
python setup.py bdist_wheel # create the package
# copy dist/package.whl to the remote
pip install -U package.whl # upgrade it in your virtualenv
# restart the server process
Upvotes: 5
Reputation: 106470
What would be involved in setting up your production server to deploy everything it needs without packaging it up?
If you did package your webapp, you can mitigate or eliminate most of these issues.
Additionally, a package is a lot more consumer-friendly. Sure, we as developers work just fine with setting up code and environments, but if you want to show something off real fast, the last thing you want to do is force someone to have to download all of the dependencies and prerequisites before they can run your app.
Upvotes: 2