Skip to content
Myr

distribute.rst

This file

Path

docs/patterns/distribute.rst

177 lines · 6.6 KiB

Last changed by

Merge pull request #2709 from davidism/docs-themeDavid Lordbefore adoption5fba092

History of this file

Take it

The walk stopped after 200 commits: lines older than c74f469 point at it.

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 1–175why, one step back
1.. _distribute-deployment:
23Deploying with Setuptools4=========================56`Setuptools`_, is an extension library that is commonly used to7distribute Python libraries and extensions. It extends distutils, a basic8module installation system shipped with Python to also support various more9complex constructs that make larger applications easier to distribute:1011- **support for dependencies**: a library or application can declare a12 list of other libraries it depends on which will be installed13 automatically for you.14- **package registry**: setuptools registers your package with your15 Python installation. This makes it possible to query information16 provided by one package from another package. The best known feature of17 this system is the entry point support which allows one package to18 declare an "entry point" that another package can hook into to extend the19 other package.20- **installation manager**: :command:`pip` can install other libraries for you.2122If you have Python 2 (>=2.7.9) or Python 3 (>=3.4) installed from python.org,23you will already have pip and setuptools on your system. Otherwise, you24will need to install them yourself.2526Flask itself, and all the libraries you can find on PyPI are distributed with27either setuptools or distutils.2829In this case we assume your application is called30:file:`yourapplication.py` and you are not using a module, but a :ref:`package31<larger-applications>`. If you have not yet converted your application into32a package, head over to the :ref:`larger-applications` pattern to see33how this can be done.3435A working deployment with setuptools is the first step into more complex36and more automated deployment scenarios. If you want to fully automate37the process, also read the :ref:`fabric-deployment` chapter.3839Basic Setup Script40------------------4142Because you have Flask installed, you have setuptools available on your system.43Flask already depends upon setuptools.4445Standard disclaimer applies: :ref:`you better use a virtualenv46<virtualenv>`.4748Your setup code always goes into a file named :file:`setup.py` next to your49application. The name of the file is only convention, but because50everybody will look for a file with that name, you better not change it.5152A basic :file:`setup.py` file for a Flask application looks like this::5354 from setuptools import setup5556 setup(57 name='Your Application',58 version='1.0',59 long_description=__doc__,60 packages=['yourapplication'],61 include_package_data=True,62 zip_safe=False,63 install_requires=['Flask']64 )6566Please keep in mind that you have to list subpackages explicitly. If you67want setuptools to lookup the packages for you automatically, you can use68the ``find_packages`` function::6970 from setuptools import setup, find_packages7172 setup(73 ...74 packages=find_packages()75 )7677Most parameters to the ``setup`` function should be self explanatory,78``include_package_data`` and ``zip_safe`` might not be.79``include_package_data`` tells setuptools to look for a :file:`MANIFEST.in` file80and install all the entries that match as package data. We will use this81to distribute the static files and templates along with the Python module82(see :ref:`distributing-resources`). The ``zip_safe`` flag can be used to83force or prevent zip Archive creation. In general you probably don't want84your packages to be installed as zip files because some tools do not85support them and they make debugging a lot harder.868788Tagging Builds89--------------9091It is useful to distinguish between release and development builds. Add a92:file:`setup.cfg` file to configure these options. ::9394 [egg_info]95 tag_build = .dev96 tag_date = 19798 [aliases]99 release = egg_info -Db ''100101Running ``python setup.py sdist`` will create a development package102with ".dev" and the current date appended: ``flaskr-1.0.dev20160314.tar.gz``.103Running ``python setup.py release sdist`` will create a release package104with only the version: ``flaskr-1.0.tar.gz``.105106107.. _distributing-resources:108109Distributing Resources110----------------------111112If you try to install the package you just created, you will notice that113folders like :file:`static` or :file:`templates` are not installed for you. The114reason for this is that setuptools does not know which files to add for115you. What you should do, is to create a :file:`MANIFEST.in` file next to your116:file:`setup.py` file. This file lists all the files that should be added to117your tarball::118119 recursive-include yourapplication/templates *120 recursive-include yourapplication/static *121122Don't forget that even if you enlist them in your :file:`MANIFEST.in` file, they123won't be installed for you unless you set the `include_package_data`124parameter of the ``setup`` function to ``True``!125126127Declaring Dependencies128----------------------129130Dependencies are declared in the ``install_requires`` parameter as a list.131Each item in that list is the name of a package that should be pulled from132PyPI on installation. By default it will always use the most recent133version, but you can also provide minimum and maximum version134requirements. Here some examples::135136 install_requires=[137 'Flask>=0.2',138 'SQLAlchemy>=0.6',139 'BrokenPackage>=0.7,<=1.0'140 ]141142As mentioned earlier, dependencies are pulled from PyPI. What if you143want to depend on a package that cannot be found on PyPI and won't be144because it is an internal package you don't want to share with anyone?145Just do it as if there was a PyPI entry and provide a list of146alternative locations where setuptools should look for tarballs::147148 dependency_links=['http://example.com/yourfiles']149150Make sure that page has a directory listing and the links on the page are151pointing to the actual tarballs with their correct filenames as this is152how setuptools will find the files. If you have an internal company153server that contains the packages, provide the URL to that server.154155156Installing / Developing157-----------------------158159To install your application (ideally into a virtualenv) just run the160:file:`setup.py` script with the ``install`` parameter. It will install your161application into the virtualenv's site-packages folder and also download162and install all dependencies::163164 $ python setup.py install165166If you are developing on the package and also want the requirements to be167installed, you can use the ``develop`` command instead::168169 $ python setup.py develop170171This has the advantage of just installing a link to the site-packages172folder instead of copying the data over. You can then continue to work on173the code without having to run ``install`` again after each change.174175

Merge pull request #2709 from davidism/docs-themeDavid Lordbefore adoption5fba092

lines 176–177why, one step back
176.. _pip: https://pypi.org/project/pip/
177.. _Setuptools: https://pypi.org/project/setuptools/