distribute.rst
This file
Path
docs/patterns/distribute.rst
166 lines · 6.7 KiB
Last changed by
Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
History of this fileTake it
The walk stopped after 200 commits: lines older than 5693b9d point at it.
Merge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 1–20why, one step back1.. _distribute-deployment:23Deploying with Distribute4=========================56`distribute`_, formerly setuptools, is an extension library that is7commonly used to (like the name says) distribute Python libraries and8extensions. It extends distutils, a basic module installation system9shipped with Python to also support various more complex constructs that10make larger applications easier to distribute:1112- **support for dependencies**: a library or application can declare a13 list of other libraries it depends on which will be installed14 automatically for you.15- **package registry**: setuptools registers your package with your16 Python installation. This makes it possible to query information17 provided by one package from another package. The best known feature of18 this system is the entry point support which allows one package to19 declare an "entry point" another package can hook into to extend the20 other package.Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 21why, one step back21- **installation manager**: :command:`easy_install`, which comes with distributeMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
line 22why, one step back22 can install other libraries for you. You can also use `pip`_ whichMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 23why, one step back23 sooner or later will replace :command:`easy_install` which does more than justMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 24–29why, one step back24 installing packages for you.2526Flask itself, and all the libraries you can find on the cheeseshop27are distributed with either distribute, the older setuptools or distutils.2829In this case we assume your application is calledMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 30why, one step back30:file:`yourapplication.py` and you are not using a module, but a :ref:`packageMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 31–50why, one step back31<larger-applications>`. Distributing resources with standard modules is32not supported by `distribute`_ so we will not bother with it. If you have33not yet converted your application into a package, head over to the34:ref:`larger-applications` pattern to see how this can be done.3536A working deployment with distribute is the first step into more complex37and more automated deployment scenarios. If you want to fully automate38the process, also read the :ref:`fabric-deployment` chapter.3940Basic Setup Script41------------------4243Because you have Flask running, you either have setuptools or distribute44available on your system anyways. If you do not, fear not, there is a45script to install it for you: `distribute_setup.py`_. Just download and46run with your Python interpreter.4748Standard disclaimer applies: :ref:`you better use a virtualenv49<virtualenv>`.50Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 51why, one step back51Your setup code always goes into a file named :file:`setup.py` next to yourMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 52–58why, one step back52application. The name of the file is only convention, but because53everybody will look for a file with that name, you better not change it.5455Yes, even if you are using `distribute`, you are importing from a package56called `setuptools`. `distribute` is fully backwards compatible with57`setuptools`, so it also uses the same import name.58Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 59why, one step back59A basic :file:`setup.py` file for a Flask application looks like this::Merge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 60–85why, one step back6061 from setuptools import setup6263 setup(64 name='Your Application',65 version='1.0',66 long_description=__doc__,67 packages=['yourapplication'],68 include_package_data=True,69 zip_safe=False,70 install_requires=['Flask']71 )7273Please keep in mind that you have to list subpackages explicitly. If you74want distribute to lookup the packages for you automatically, you can use75the `find_packages` function::7677 from setuptools import setup, find_packages7879 setup(80 ...81 packages=find_packages()82 )8384Most parameters to the `setup` function should be self explanatory,85`include_package_data` and `zip_safe` might not be.Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 86why, one step back86`include_package_data` tells distribute to look for a :file:`MANIFEST.in` fileMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 87–100why, one step back87and install all the entries that match as package data. We will use this88to distribute the static files and templates along with the Python module89(see :ref:`distributing-resources`). The `zip_safe` flag can be used to90force or prevent zip Archive creation. In general you probably don't want91your packages to be installed as zip files because some tools do not92support them and they make debugging a lot harder.939495.. _distributing-resources:9697Distributing Resources98----------------------99100If you try to install the package you just created, you will notice thatMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 101why, one step back101folders like :file:`static` or :file:`templates` are not installed for you. TheMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
line 102why, one step back102reason for this is that distribute does not know which files to add forMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
lines 103–104why, one step back103you. What you should do, is to create a :file:`MANIFEST.in` file next to your104:file:`setup.py` file. This file lists all the files that should be added toMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 105–109why, one step back105your tarball::106107 recursive-include yourapplication/templates *108 recursive-include yourapplication/static *109Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 110why, one step back110Don't forget that even if you enlist them in your :file:`MANIFEST.in` file, theyMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
line 111why, one step back111won't be installed for you unless you set the `include_package_data`Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 112why, one step back112parameter of the `setup` function to ``True``!Merge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 113–147why, one step back113114115Declaring Dependencies116----------------------117118Dependencies are declared in the `install_requires` parameter as list.119Each item in that list is the name of a package that should be pulled from120PyPI on installation. By default it will always use the most recent121version, but you can also provide minimum and maximum version122requirements. Here some examples::123124 install_requires=[125 'Flask>=0.2',126 'SQLAlchemy>=0.6',127 'BrokenPackage>=0.7,<=1.0'128 ]129130I mentioned earlier that dependencies are pulled from PyPI. What if you131want to depend on a package that cannot be found on PyPI and won't be132because it is an internal package you don't want to share with anyone?133Just still do as if there was a PyPI entry for it and provide a list of134alternative locations where distribute should look for tarballs::135136 dependency_links=['http://example.com/yourfiles']137138Make sure that page has a directory listing and the links on the page are139pointing to the actual tarballs with their correct filenames as this is140how distribute will find the files. If you have an internal company141server that contains the packages, provide the URL to that server there.142143144Installing / Developing145-----------------------146147To install your application (ideally into a virtualenv) just run theMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 148why, one step back148:file:`setup.py` script with the `install` parameter. It will install yourMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 149–166why, one step back149application into the virtualenv's site-packages folder and also download150and install all dependencies::151152 $ python setup.py install153154If you are developing on the package and also want the requirements to be155installed, you can use the `develop` command instead::156157 $ python setup.py develop158159This has the advantage of just installing a link to the site-packages160folder instead of copying the data over. You can then continue to work on161the code without having to run `install` again after each change.162163164.. _distribute: https://pypi.python.org/pypi/distribute165.. _pip: https://pypi.python.org/pypi/pip166.. _distribute_setup.py: http://python-distribute.org/distribute_setup.py