distribute.rst
This file
Path
docs/patterns/distribute.rst
177 lines · 6.6 KiB
Last changed by
clean up code formatting in some docsDavid Lordbefore adoption1928f28
History of this fileTake it
The walk stopped after 200 commits: lines older than f63cd72 point at it.
Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 1–6why, one step back1.. _distribute-deployment:23Deploying with Setuptools4=========================56`Setuptools`_, is an extension library that is commonly used toMerge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9
lines 7–9why, one step back7distribute 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:Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 10–17why, one step back1011- **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 toMerge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9
line 18why, one step back18 declare an "entry point" that another package can hook into to extend theMerge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
line 19why, one step back19 other package.Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9
line 20why, one step back20- **installation manager**: :command:`pip` can install other libraries for you.Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
line 21why, one step back21Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9
lines 22–24why, one step back22If 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.Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
line 25why, one step back25Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9
lines 26–28why, one step back26Flask itself, and all the libraries you can find on PyPI are distributed with27either setuptools or distutils.28Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 29–34why, one step back29In 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.34Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9
line 35why, one step back35A working deployment with setuptools is the first step into more complexMerge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 36–41why, one step back36and more automated deployment scenarios. If you want to fully automate37the process, also read the :ref:`fabric-deployment` chapter.3839Basic Setup Script40------------------41clean up code formatting in some docsDavid Lordbefore adoption1928f28
lines 42–43why, one step back42Because you have Flask installed, you have setuptools available on your system.43Flask already depends upon setuptools.Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 44–66why, one step back4445Standard 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 youMerge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9
line 67why, one step back67want setuptools to lookup the packages for you automatically, you can useclean up code formatting in some docsDavid Lordbefore adoption1928f28
line 68why, one step back68the ``find_packages`` function::Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 69–76why, one step back6970 from setuptools import setup, find_packages7172 setup(73 ...74 packages=find_packages()75 )76clean up code formatting in some docsDavid Lordbefore adoption1928f28
lines 77–79why, one step back77Most 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` fileMerge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 80–81why, one step back80and install all the entries that match as package data. We will use this81to distribute the static files and templates along with the Python moduleclean up code formatting in some docsDavid Lordbefore adoption1928f28
line 82why, one step back82(see :ref:`distributing-resources`). The ``zip_safe`` flag can be used toMerge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 83–87why, one step back83force 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.8687move setup.cfg info to setuptools docs, rewordDavid Lordbefore adoption14a5a9e
lines 88–106why, one step back88Tagging 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 -RDb ''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``.105106Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 107–113why, one step back107.. _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. TheMerge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9
line 114why, one step back114reason for this is that setuptools does not know which files to add forMerge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 115–123why, one step back115you. 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`clean up code formatting in some docsDavid Lordbefore adoption1928f28
line 124why, one step back124parameter of the ``setup`` function to ``True``!Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 125–129why, one step back125126127Declaring Dependencies128----------------------129clean up code formatting in some docsDavid Lordbefore adoption1928f28
line 130why, one step back130Dependencies are declared in the ``install_requires`` parameter as a list.Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 131–144why, one step back131Each 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?Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9
lines 145–146why, one step back145Just do it as if there was a PyPI entry and provide a list of146alternative locations where setuptools should look for tarballs::Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 147–151why, one step back147148 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 isMerge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9
lines 152–153why, one step back152how setuptools will find the files. If you have an internal company153server that contains the packages, provide the URL to that server.Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 154–159why, one step back154155156Installing / Developing157-----------------------158159To install your application (ideally into a virtualenv) just run theclean up code formatting in some docsDavid Lordbefore adoption1928f28
line 160why, one step back160:file:`setup.py` script with the ``install`` parameter. It will install yourMerge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 161–166why, one step back161application 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 beclean up code formatting in some docsDavid Lordbefore adoption1928f28
line 167why, one step back167installed, you can use the ``develop`` command instead::Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 168–172why, one step back168169 $ 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 onclean up code formatting in some docsDavid Lordbefore adoption1928f28
line 173why, one step back173the code without having to run ``install`` again after each change.Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72
lines 174–177why, one step back174175176.. _pip: https://pypi.python.org/pypi/pip177.. _Setuptools: https://pythonhosted.org/setuptools