Skip to content
Myr

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 file

Take 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 back
1.. _distribute-deployment:
23Deploying with Setuptools4=========================56`Setuptools`_, is an extension library that is commonly used to

Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9

lines 7–9why, one step back
7distribute Python libraries and extensions. It extends distutils, a basic
8module 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 back
10
11- **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 to

Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9

line 18why, one step back
18 declare an "entry point" that another package can hook into to extend the

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

line 19why, one step back
19 other package.

Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9

line 20why, one step back
20- **installation manager**: :command:`pip` can install other libraries for you.

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

line 21why, one step back
21

Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9

lines 22–24why, one step back
22If 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 back
25

Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9

lines 26–28why, one step back
26Flask itself, and all the libraries you can find on PyPI are distributed with
27either setuptools or distutils.28

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 29–34why, one step back
29In this case we assume your application is called
30: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.34

Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9

line 35why, one step back
35A working deployment with setuptools is the first step into more complex

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 36–41why, one step back
36and more automated deployment scenarios. If you want to fully automate
37the process, also read the :ref:`fabric-deployment` chapter.3839Basic Setup Script40------------------41

clean up code formatting in some docsDavid Lordbefore adoption1928f28

lines 42–43why, one step back
42Because 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 back
44
45Standard 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 you

Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9

line 67why, one step back
67want setuptools to lookup the packages for you automatically, you can use

clean up code formatting in some docsDavid Lordbefore adoption1928f28

line 68why, one step back
68the ``find_packages`` function::

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 69–76why, one step back
69
70 from setuptools import setup, find_packages7172 setup(73 ...74 packages=find_packages()75 )76

clean up code formatting in some docsDavid Lordbefore adoption1928f28

lines 77–79why, one step back
77Most 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` file

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 80–81why, one step back
80and install all the entries that match as package data. We will use this
81to distribute the static files and templates along with the Python module

clean up code formatting in some docsDavid Lordbefore adoption1928f28

line 82why, one step back
82(see :ref:`distributing-resources`). The ``zip_safe`` flag can be used to

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 83–87why, one step back
83force or prevent zip Archive creation. In general you probably don't want
84your packages to be installed as zip files because some tools do not85support them and they make debugging a lot harder.8687

move setup.cfg info to setuptools docs, rewordDavid Lordbefore adoption14a5a9e

lines 88–106why, one step back
88Tagging Builds
89--------------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``.105106

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 107–113why, one step back
107.. _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. The

Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9

line 114why, one step back
114reason for this is that setuptools does not know which files to add for

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 115–123why, one step back
115you. What you should do, is to create a :file:`MANIFEST.in` file next to your
116: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 back
124parameter of the ``setup`` function to ``True``!

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 125–129why, one step back
125
126127Declaring Dependencies128----------------------129

clean up code formatting in some docsDavid Lordbefore adoption1928f28

line 130why, one step back
130Dependencies 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 back
131Each item in that list is the name of a package that should be pulled from
132PyPI 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 back
145Just do it as if there was a PyPI entry and provide a list of
146alternative locations where setuptools should look for tarballs::

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 147–151why, one step back
147
148 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 is

Merge pull request #1795 from cgoldberg/update-docs-setuptoolsMarkus Unterwaditzerbefore adoptione8bd2c9

lines 152–153why, one step back
152how setuptools will find the files. If you have an internal company
153server 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 back
154
155156Installing / Developing157-----------------------158159To install your application (ideally into a virtualenv) just run the

clean up code formatting in some docsDavid Lordbefore adoption1928f28

line 160why, one step back
160:file:`setup.py` script with the ``install`` parameter. It will install your

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 161–166why, one step back
161application into the virtualenv's site-packages folder and also download
162and install all dependencies::163164 $ python setup.py install165166If you are developing on the package and also want the requirements to be

clean up code formatting in some docsDavid Lordbefore adoption1928f28

line 167why, one step back
167installed, you can use the ``develop`` command instead::

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 168–172why, one step back
168
169 $ 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 on

clean up code formatting in some docsDavid Lordbefore adoption1928f28

line 173why, one step back
173the 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 back
174
175176.. _pip: https://pypi.python.org/pypi/pip177.. _Setuptools: https://pythonhosted.org/setuptools