distribute.rst
This file
Path
docs/patterns/distribute.rst
170 lines · 6.3 KiB
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 7a444c5 point at it.
update tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
lines 1–170why, one step back1Deploying with Setuptools2=========================34`Setuptools`_, is an extension library that is commonly used to5distribute Python libraries and extensions. It extends distutils, a basic6module installation system shipped with Python to also support various more7complex constructs that make larger applications easier to distribute:89- **support for dependencies**: a library or application can declare a10 list of other libraries it depends on which will be installed11 automatically for you.12- **package registry**: setuptools registers your package with your13 Python installation. This makes it possible to query information14 provided by one package from another package. The best known feature of15 this system is the entry point support which allows one package to16 declare an "entry point" that another package can hook into to extend the17 other package.18- **installation manager**: :command:`pip` can install other libraries for you.1920Flask itself, and all the libraries you can find on PyPI are distributed with21either setuptools or distutils.2223In this case we assume your application is called24:file:`yourapplication.py` and you are not using a module, but a25package. If you have not yet converted your application into a package,26head over to :doc:`packages` to see how this can be done.2728A working deployment with setuptools is the first step into more complex29and more automated deployment scenarios. If you want to fully automate30the process, also read the :doc:`fabric` chapter.3132Basic Setup Script33------------------3435Because you have Flask installed, you have setuptools available on your system.36Flask already depends upon setuptools.3738Standard disclaimer applies: :ref:`use a virtualenv39<install-create-env>`.4041Your setup code always goes into a file named :file:`setup.py` next to your42application. The name of the file is only convention, but because43everybody will look for a file with that name, you better not change it.4445A basic :file:`setup.py` file for a Flask application looks like this::4647 from setuptools import setup4849 setup(50 name='Your Application',51 version='1.0',52 long_description=__doc__,53 packages=['yourapplication'],54 include_package_data=True,55 zip_safe=False,56 install_requires=['Flask']57 )5859Please keep in mind that you have to list subpackages explicitly. If you60want setuptools to lookup the packages for you automatically, you can use61the ``find_packages`` function::6263 from setuptools import setup, find_packages6465 setup(66 ...67 packages=find_packages()68 )6970Most parameters to the ``setup`` function should be self explanatory,71``include_package_data`` and ``zip_safe`` might not be.72``include_package_data`` tells setuptools to look for a :file:`MANIFEST.in` file73and install all the entries that match as package data. We will use this74to distribute the static files and templates along with the Python module75(see :ref:`distributing-resources`). The ``zip_safe`` flag can be used to76force or prevent zip Archive creation. In general you probably don't want77your packages to be installed as zip files because some tools do not78support them and they make debugging a lot harder.798081Tagging Builds82--------------8384It is useful to distinguish between release and development builds. Add a85:file:`setup.cfg` file to configure these options. ::8687 [egg_info]88 tag_build = .dev89 tag_date = 19091 [aliases]92 release = egg_info -Db ''9394Running ``python setup.py sdist`` will create a development package95with ".dev" and the current date appended: ``flaskr-1.0.dev20160314.tar.gz``.96Running ``python setup.py release sdist`` will create a release package97with only the version: ``flaskr-1.0.tar.gz``.9899100.. _distributing-resources:101102Distributing Resources103----------------------104105If you try to install the package you just created, you will notice that106folders like :file:`static` or :file:`templates` are not installed for you. The107reason for this is that setuptools does not know which files to add for108you. What you should do, is to create a :file:`MANIFEST.in` file next to your109:file:`setup.py` file. This file lists all the files that should be added to110your tarball::111112 recursive-include yourapplication/templates *113 recursive-include yourapplication/static *114115Don't forget that even if you enlist them in your :file:`MANIFEST.in` file, they116won't be installed for you unless you set the `include_package_data`117parameter of the ``setup`` function to ``True``!118119120Declaring Dependencies121----------------------122123Dependencies are declared in the ``install_requires`` parameter as a list.124Each item in that list is the name of a package that should be pulled from125PyPI on installation. By default it will always use the most recent126version, but you can also provide minimum and maximum version127requirements. Here some examples::128129 install_requires=[130 'Flask>=0.2',131 'SQLAlchemy>=0.6',132 'BrokenPackage>=0.7,<=1.0'133 ]134135As mentioned earlier, dependencies are pulled from PyPI. What if you136want to depend on a package that cannot be found on PyPI and won't be137because it is an internal package you don't want to share with anyone?138Just do it as if there was a PyPI entry and provide a list of139alternative locations where setuptools should look for tarballs::140141 dependency_links=['http://example.com/yourfiles']142143Make sure that page has a directory listing and the links on the page are144pointing to the actual tarballs with their correct filenames as this is145how setuptools will find the files. If you have an internal company146server that contains the packages, provide the URL to that server.147148149Installing / Developing150-----------------------151152To install your application (ideally into a virtualenv) just run the153:file:`setup.py` script with the ``install`` parameter. It will install your154application into the virtualenv's site-packages folder and also download155and install all dependencies::156157 $ python setup.py install158159If you are developing on the package and also want the requirements to be160installed, you can use the ``develop`` command instead::161162 $ python setup.py develop163164This has the advantage of just installing a link to the site-packages165folder instead of copying the data over. You can then continue to work on166the code without having to run ``install`` again after each change.167168169.. _pip: https://pypi.org/project/pip/170.. _Setuptools: https://pypi.org/project/setuptools/