官术网_书友最值得收藏!

  • Flask Framework Cookbook
  • Shalabh Aggarwal
  • 333字
  • 2021-08-05 17:17:21

Making a Flask app installable using setuptools

So, we have a Flask application now, but how do we install it just like any Python package? It is possible that any other application depends on our application or our application is in fact an extension for Flask and would need to be installed in a Python environment so that it can be used by other applications.

How to do it…

Installing a Flask app can be achieved very easily using the setuptools library of Python. We will have to create a file called setup.py in our application's folder and configure it to run a setup script for our application. It will take care of any dependencies, descriptions, loading test packages, and so on.

The following is an example of a simple setup.py script for our Hello World application:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
from setuptools import setup

setup(
    name = 'my_app',
    version='1.0',
    license='GNU General Public License v3',
    author='Shalabh Aggarwal',
    author_email='contact@shalabhaggarwal.com',
    description='Hello world application for Flask',
    packages=['my_app'],
    platforms='any',
    install_requires=[
        'flask',
    ],
    classifiers=[
        'Development Status :: 4 - Beta',
        'Environment :: Web Environment',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: GNU General Public License v3',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
        'Topic :: Software Development :: Libraries :: Python Modules'
    ],
)

How it works…

In the preceding script, most of the configuration is self-explanatory. The classifiers are used when we make this application available on PyPI. These will help other users search the application using these classifiers.

Now, we can just run this file with the install keyword as shown here:

$ python setup.py install

This will install this application along with all its dependencies mentioned in install_requires, that is, Flask and all the dependencies of Flask as well. Then, this app can be used just like any Python package in our Python environment.

See also

主站蜘蛛池模板: 花莲县| 阿鲁科尔沁旗| 大安市| 龙州县| 抚顺市| 临夏县| 禄丰县| 九台市| 贵德县| 岳普湖县| 东乌| 河东区| 澄城县| 寿光市| 绥芬河市| 若尔盖县| 任丘市| 潞西市| 玉屏| 安达市| 丰城市| 小金县| 日喀则市| 基隆市| 白银市| 遂川县| 津市市| 鄢陵县| 佛学| 新绛县| 雅安市| 四川省| 威海市| 汉沽区| 岳阳县| 剑川县| 文水县| 都匀市| 福鼎市| 肇庆市| 工布江达县|