distribute.rst
This file
Path
docs/patterns/distribute.rst
166 lines · 6.6 KiB
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 22af78a point at it.
Removed outdated sentence in the testing docsArmin Ronacherbefore adoption22af78a
lines 1–166why, 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.21- **installation manager**: `easy_install`, which comes with distribute22 can install other libraries for you. You can also use `pip`_ which23 sooner or later will replace `easy_install` which does more than just24 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 called30`yourapplication.py` and you are not using a module, but a :ref:`package31<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>`.5051Your setup code always goes into a file named `setup.py` next to your52application. 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.5859A basic `setup.py` file for a Flask application looks like this::6061 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.86`include_package_data` tells distribute to look for a `MANIFEST.in` file87and 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 that101folders like `static` or `templates` are not installed for you. The102reason for this is that distribute does not know which files to add for103you. What you should do, is to create a `MANIFEST.in` file next to your104`setup.py` file. This file lists all the files that should be added to105your tarball::106107 recursive-include yourapplication/templates *108 recursive-include yourapplication/static *109110Don't forget that even if you enlist them in your `MANIFEST.in` file, they111won't be installed for you unless you set the `include_package_data`112parameter of the `setup` function to `True`!113114115Declaring 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 the148`setup.py` script with the `install` parameter. It will install your149application 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: http://pypi.python.org/pypi/distribute165.. _pip: http://pypi.python.org/pypi/pip166.. _distribute_setup.py: http://python-distribute.org/distribute_setup.py