| 
									
										
										
										
											2010-05-27 23:42:01 +08:00
										 |  |  | .. _distribute-deployment:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-20 17:10:55 +08:00
										 |  |  | Deploying with Setuptools
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | =========================
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-20 17:10:55 +08:00
										 |  |  | `Setuptools`_, is an extension library that is commonly used to
 | 
					
						
							| 
									
										
										
										
											2016-05-03 23:55:36 +08:00
										 |  |  | distribute Python libraries and extensions. It extends distutils, a basic
 | 
					
						
							|  |  |  | module installation system shipped with Python to also support various more
 | 
					
						
							|  |  |  | complex constructs that make larger applications easier to distribute:
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | - **support for dependencies**: a library or application can declare a
 | 
					
						
							|  |  |  |   list of other libraries it depends on which will be installed
 | 
					
						
							|  |  |  |   automatically for you.
 | 
					
						
							|  |  |  | - **package registry**: setuptools registers your package with your
 | 
					
						
							|  |  |  |   Python installation.  This makes it possible to query information
 | 
					
						
							|  |  |  |   provided by one package from another package.  The best known feature of
 | 
					
						
							|  |  |  |   this system is the entry point support which allows one package to
 | 
					
						
							| 
									
										
										
										
											2016-05-03 23:55:36 +08:00
										 |  |  |   declare an "entry point" that another package can hook into to extend the
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  |   other package.
 | 
					
						
							| 
									
										
										
										
											2016-05-05 09:14:25 +08:00
										 |  |  | - **installation manager**: :command:`pip` can install other libraries for you.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If you have Python 2 (>=2.7.9) or Python 3 (>=3.4) installed from python.org,
 | 
					
						
							|  |  |  | you will already have pip and setuptools on your system.  Otherwise, you
 | 
					
						
							|  |  |  | will need to install them yourself.
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 23:55:36 +08:00
										 |  |  | Flask itself, and all the libraries you can find on PyPI are distributed with
 | 
					
						
							|  |  |  | either setuptools or distutils.
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | In this case we assume your application is called
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:45:22 +08:00
										 |  |  | :file:`yourapplication.py` and you are not using a module, but a :ref:`package
 | 
					
						
							| 
									
										
										
										
											2015-10-20 17:10:55 +08:00
										 |  |  | <larger-applications>`. If you have not yet converted your application into
 | 
					
						
							|  |  |  | a package, head over to the :ref:`larger-applications` pattern to see
 | 
					
						
							|  |  |  | how this can be done.
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 23:55:36 +08:00
										 |  |  | A working deployment with setuptools is the first step into more complex
 | 
					
						
							| 
									
										
										
										
											2010-07-06 22:07:13 +08:00
										 |  |  | and more automated deployment scenarios.  If you want to fully automate
 | 
					
						
							|  |  |  | the process, also read the :ref:`fabric-deployment` chapter.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | Basic Setup Script
 | 
					
						
							|  |  |  | ------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-27 04:03:29 +08:00
										 |  |  | Because you have Flask installed, you have setuptools available on your system.
 | 
					
						
							|  |  |  | Flask already depends upon setuptools.
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Standard disclaimer applies: :ref:`you better use a virtualenv
 | 
					
						
							|  |  |  | <virtualenv>`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:45:22 +08:00
										 |  |  | Your setup code always goes into a file named :file:`setup.py` next to your
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | application.  The name of the file is only convention, but because
 | 
					
						
							|  |  |  | everybody will look for a file with that name, you better not change it.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:45:22 +08:00
										 |  |  | A basic :file:`setup.py` file for a Flask application looks like this::
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     from setuptools import setup
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setup(
 | 
					
						
							|  |  |  |         name='Your Application',
 | 
					
						
							|  |  |  |         version='1.0',
 | 
					
						
							|  |  |  |         long_description=__doc__,
 | 
					
						
							|  |  |  |         packages=['yourapplication'],
 | 
					
						
							|  |  |  |         include_package_data=True,
 | 
					
						
							|  |  |  |         zip_safe=False,
 | 
					
						
							|  |  |  |         install_requires=['Flask']
 | 
					
						
							|  |  |  |     )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Please keep in mind that you have to list subpackages explicitly.  If you
 | 
					
						
							| 
									
										
										
										
											2016-05-03 23:55:36 +08:00
										 |  |  | want setuptools to lookup the packages for you automatically, you can use
 | 
					
						
							| 
									
										
										
										
											2016-06-27 04:03:29 +08:00
										 |  |  | the ``find_packages`` function::
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     from setuptools import setup, find_packages
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setup(
 | 
					
						
							|  |  |  |         ...
 | 
					
						
							|  |  |  |         packages=find_packages()
 | 
					
						
							|  |  |  |     )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-27 04:03:29 +08:00
										 |  |  | Most parameters to the ``setup`` function should be self explanatory,
 | 
					
						
							|  |  |  | ``include_package_data`` and ``zip_safe`` might not be.
 | 
					
						
							|  |  |  | ``include_package_data`` tells setuptools to look for a :file:`MANIFEST.in` file
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | and install all the entries that match as package data.  We will use this
 | 
					
						
							|  |  |  | to distribute the static files and templates along with the Python module
 | 
					
						
							| 
									
										
										
										
											2016-06-27 04:03:29 +08:00
										 |  |  | (see :ref:`distributing-resources`).  The ``zip_safe`` flag can be used to
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | force or prevent zip Archive creation.  In general you probably don't want
 | 
					
						
							|  |  |  | your packages to be installed as zip files because some tools do not
 | 
					
						
							|  |  |  | support them and they make debugging a lot harder.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-06 03:59:04 +08:00
										 |  |  | Tagging Builds
 | 
					
						
							|  |  |  | --------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 10:55:39 +08:00
										 |  |  | It is useful to distinguish between release and development builds. Add a
 | 
					
						
							|  |  |  | :file:`setup.cfg` file to configure these options. ::
 | 
					
						
							| 
									
										
										
										
											2016-06-06 03:59:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     [egg_info]
 | 
					
						
							|  |  |  |     tag_build = .dev
 | 
					
						
							|  |  |  |     tag_date = 1
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     [aliases]
 | 
					
						
							| 
									
										
										
										
											2017-07-27 22:32:03 +08:00
										 |  |  |     release = egg_info -Db ''
 | 
					
						
							| 
									
										
										
										
											2016-06-06 03:59:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Running ``python setup.py sdist`` will create a development package
 | 
					
						
							|  |  |  | with ".dev" and the current date appended: ``flaskr-1.0.dev20160314.tar.gz``.
 | 
					
						
							|  |  |  | Running ``python setup.py release sdist`` will create a release package
 | 
					
						
							|  |  |  | with only the version: ``flaskr-1.0.tar.gz``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | .. _distributing-resources:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Distributing Resources
 | 
					
						
							|  |  |  | ----------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If you try to install the package you just created, you will notice that
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:45:22 +08:00
										 |  |  | folders like :file:`static` or :file:`templates` are not installed for you.  The
 | 
					
						
							| 
									
										
										
										
											2016-05-03 23:55:36 +08:00
										 |  |  | reason for this is that setuptools does not know which files to add for
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:45:22 +08:00
										 |  |  | you.  What you should do, is to create a :file:`MANIFEST.in` file next to your
 | 
					
						
							|  |  |  | :file:`setup.py` file.  This file lists all the files that should be added to
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | your tarball::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-18 16:57:26 +08:00
										 |  |  |     recursive-include yourapplication/templates *
 | 
					
						
							|  |  |  |     recursive-include yourapplication/static *
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:45:22 +08:00
										 |  |  | Don't forget that even if you enlist them in your :file:`MANIFEST.in` file, they
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | won't be installed for you unless you set the `include_package_data`
 | 
					
						
							| 
									
										
										
										
											2016-06-27 04:03:29 +08:00
										 |  |  | parameter of the ``setup`` function to ``True``!
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Declaring Dependencies
 | 
					
						
							|  |  |  | ----------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-27 04:03:29 +08:00
										 |  |  | Dependencies are declared in the ``install_requires`` parameter as a list.
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | Each item in that list is the name of a package that should be pulled from
 | 
					
						
							|  |  |  | PyPI on installation.  By default it will always use the most recent
 | 
					
						
							|  |  |  | version, but you can also provide minimum and maximum version
 | 
					
						
							|  |  |  | requirements.  Here some examples::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     install_requires=[
 | 
					
						
							|  |  |  |         'Flask>=0.2',
 | 
					
						
							|  |  |  |         'SQLAlchemy>=0.6',
 | 
					
						
							|  |  |  |         'BrokenPackage>=0.7,<=1.0'
 | 
					
						
							|  |  |  |     ]
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-01 11:57:35 +08:00
										 |  |  | As mentioned earlier, dependencies are pulled from PyPI.  What if you
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | want to depend on a package that cannot be found on PyPI and won't be
 | 
					
						
							|  |  |  | because it is an internal package you don't want to share with anyone?
 | 
					
						
							| 
									
										
										
										
											2016-05-03 23:55:36 +08:00
										 |  |  | Just do it as if there was a PyPI entry and provide a list of
 | 
					
						
							|  |  |  | alternative locations where setuptools should look for tarballs::
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     dependency_links=['http://example.com/yourfiles']
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Make sure that page has a directory listing and the links on the page are
 | 
					
						
							|  |  |  | pointing to the actual tarballs with their correct filenames as this is
 | 
					
						
							| 
									
										
										
										
											2016-05-03 23:55:36 +08:00
										 |  |  | how setuptools will find the files.  If you have an internal company
 | 
					
						
							|  |  |  | server that contains the packages, provide the URL to that server.
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Installing / Developing
 | 
					
						
							|  |  |  | -----------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | To install your application (ideally into a virtualenv) just run the
 | 
					
						
							| 
									
										
										
										
											2016-06-27 04:03:29 +08:00
										 |  |  | :file:`setup.py` script with the ``install`` parameter.  It will install your
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | application into the virtualenv's site-packages folder and also download
 | 
					
						
							|  |  |  | and install all dependencies::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $ python setup.py install
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If you are developing on the package and also want the requirements to be
 | 
					
						
							| 
									
										
										
										
											2016-06-27 04:03:29 +08:00
										 |  |  | installed, you can use the ``develop`` command instead::
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $ python setup.py develop
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This has the advantage of just installing a link to the site-packages
 | 
					
						
							|  |  |  | folder instead of copying the data over.  You can then continue to work on
 | 
					
						
							| 
									
										
										
										
											2016-06-27 04:03:29 +08:00
										 |  |  | the code without having to run ``install`` again after each change.
 | 
					
						
							| 
									
										
										
										
											2010-05-17 07:41:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 13:54:11 +08:00
										 |  |  | .. _pip: https://pypi.org/project/pip/
 | 
					
						
							|  |  |  | .. _Setuptools: https://pypi.org/project/setuptools/
 |