2010-04-09 07:32:39 +08:00
|
|
|
.. _installation:
|
|
|
|
|
|
|
|
Installation
|
|
|
|
============
|
|
|
|
|
2014-02-11 04:24:31 +08:00
|
|
|
Flask depends on some external libraries, like `Werkzeug
|
2014-04-26 07:20:12 +08:00
|
|
|
<http://werkzeug.pocoo.org/>`_ and `Jinja2 <http://jinja.pocoo.org/>`_.
|
2010-06-26 14:39:16 +08:00
|
|
|
Werkzeug is a toolkit for WSGI, the standard Python interface between web
|
|
|
|
applications and a variety of servers for both development and deployment.
|
|
|
|
Jinja2 renders templates.
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2011-06-26 10:14:34 +08:00
|
|
|
So how do you get all that on your computer quickly? There are many ways you
|
|
|
|
could do that, but the most kick-ass method is virtualenv, so let's have a look
|
|
|
|
at that first.
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2014-02-11 04:24:31 +08:00
|
|
|
You will need Python 2.6 or newer to get started, so be sure to have an
|
2013-06-09 19:42:24 +08:00
|
|
|
up-to-date Python 2.x installation. For using Flask with Python 3 have a
|
|
|
|
look at :ref:`python3-support`.
|
2010-07-18 06:51:20 +08:00
|
|
|
|
2010-05-17 07:41:57 +08:00
|
|
|
.. _virtualenv:
|
|
|
|
|
2010-04-09 19:13:39 +08:00
|
|
|
virtualenv
|
|
|
|
----------
|
|
|
|
|
2011-06-26 10:14:34 +08:00
|
|
|
Virtualenv is probably what you want to use during development, and if you have
|
|
|
|
shell access to your production machines, you'll probably want to use it there,
|
|
|
|
too.
|
2010-06-26 14:39:16 +08:00
|
|
|
|
2011-06-26 10:14:34 +08:00
|
|
|
What problem does virtualenv solve? If you like Python as much as I do,
|
|
|
|
chances are you want to use it for other projects besides Flask-based web
|
|
|
|
applications. But the more projects you have, the more likely it is that you
|
|
|
|
will be working with different versions of Python itself, or at least different
|
|
|
|
versions of Python libraries. Let's face it: quite often libraries break
|
|
|
|
backwards compatibility, and it's unlikely that any serious application will
|
|
|
|
have zero dependencies. So what do you do if two or more of your projects have
|
|
|
|
conflicting dependencies?
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2011-06-26 10:14:34 +08:00
|
|
|
Virtualenv to the rescue! Virtualenv enables multiple side-by-side
|
|
|
|
installations of Python, one for each project. It doesn't actually install
|
|
|
|
separate copies of Python, but it does provide a clever way to keep different
|
|
|
|
project environments isolated. Let's see how virtualenv works.
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2017-03-20 01:01:23 +08:00
|
|
|
|
|
|
|
.. admonition:: A note on python3 and virtualenv
|
|
|
|
|
|
|
|
If you are planning on using python3 with the virtualenv, you don't need to
|
|
|
|
install ``virtualenv``. Python3 has built-in support for virtual environments.
|
|
|
|
|
2016-07-06 03:00:43 +08:00
|
|
|
If you are on Mac OS X or Linux, chances are that the following
|
|
|
|
command will work for you::
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2010-04-11 08:20:10 +08:00
|
|
|
$ sudo pip install virtualenv
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2016-07-06 03:00:43 +08:00
|
|
|
It will probably install virtualenv on your system. Maybe it's even
|
2011-06-26 10:14:34 +08:00
|
|
|
in your package manager. If you use Ubuntu, try::
|
2010-06-26 14:39:16 +08:00
|
|
|
|
|
|
|
$ sudo apt-get install python-virtualenv
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2016-07-06 03:00:43 +08:00
|
|
|
If you are on Windows and don't have the ``easy_install`` command, you must
|
2010-04-11 08:20:10 +08:00
|
|
|
install it first. Check the :ref:`windows-easy-install` section for more
|
2011-06-26 10:14:34 +08:00
|
|
|
information about how to do that. Once you have it installed, run the same
|
2016-07-06 03:00:43 +08:00
|
|
|
commands as above, but without the ``sudo`` prefix.
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2017-03-20 01:01:23 +08:00
|
|
|
Creating a virtual environment
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2010-06-26 14:39:16 +08:00
|
|
|
Once you have virtualenv installed, just fire up a shell and create
|
2017-03-20 01:01:23 +08:00
|
|
|
your own environment. I usually create a project folder and a :file:`virtenv`
|
2010-06-26 14:39:16 +08:00
|
|
|
folder within::
|
2010-04-09 19:13:39 +08:00
|
|
|
|
|
|
|
$ mkdir myproject
|
|
|
|
$ cd myproject
|
2017-03-20 01:01:23 +08:00
|
|
|
|
|
|
|
There is a little change in how you create a virtualenv depending on which python-version you are currently using.
|
|
|
|
|
|
|
|
**Python2**
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
$ virtualenv virtenv
|
|
|
|
New python executable in virtenv/bin/python
|
2014-02-11 04:24:31 +08:00
|
|
|
Installing setuptools, pip............done.
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2017-03-20 01:01:23 +08:00
|
|
|
**Python 3.6 and above**
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
$ python3 -m venv virtenv
|
|
|
|
|
|
|
|
Activating a virtual environment
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2011-06-26 10:14:34 +08:00
|
|
|
Now, whenever you want to work on a project, you only have to activate the
|
|
|
|
corresponding environment. On OS X and Linux, do the following::
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2017-03-20 01:01:23 +08:00
|
|
|
$ . virtenv/bin/activate
|
2010-04-09 19:13:39 +08:00
|
|
|
|
|
|
|
If you are a Windows user, the following command is for you::
|
|
|
|
|
2017-03-20 01:01:23 +08:00
|
|
|
$ virtenv\Scripts\activate
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2011-06-26 10:14:34 +08:00
|
|
|
Either way, you should now be using your virtualenv (notice how the prompt of
|
2011-06-26 10:17:37 +08:00
|
|
|
your shell has changed to show the active environment).
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2014-06-16 18:39:16 +08:00
|
|
|
And if you want to go back to the real world, use the following command::
|
|
|
|
|
|
|
|
$ deactivate
|
|
|
|
|
2014-12-25 11:52:11 +08:00
|
|
|
After doing this, the prompt of your shell should be as familiar as before.
|
2014-06-16 18:39:16 +08:00
|
|
|
|
2014-06-16 18:59:33 +08:00
|
|
|
Now, let's move on. Enter the following command to get Flask activated in your
|
2011-06-26 10:14:34 +08:00
|
|
|
virtualenv::
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2012-02-07 09:41:13 +08:00
|
|
|
$ pip install Flask
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2011-06-26 10:14:34 +08:00
|
|
|
A few seconds later and you are good to go.
|
2010-04-09 19:13:39 +08:00
|
|
|
|
|
|
|
|
2011-12-24 17:55:52 +08:00
|
|
|
System-Wide Installation
|
2010-04-09 19:13:39 +08:00
|
|
|
------------------------
|
|
|
|
|
2011-12-24 17:55:52 +08:00
|
|
|
This is possible as well, though I do not recommend it. Just run
|
2016-07-06 03:00:43 +08:00
|
|
|
``pip`` with root privileges::
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2012-02-07 09:28:17 +08:00
|
|
|
$ sudo pip install Flask
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2011-06-26 10:14:34 +08:00
|
|
|
(On Windows systems, run it in a command-prompt window with administrator
|
2016-07-06 03:00:43 +08:00
|
|
|
privileges, and leave out ``sudo``.)
|
2010-04-09 19:13:39 +08:00
|
|
|
|
2010-04-19 23:03:08 +08:00
|
|
|
|
2010-04-19 23:05:24 +08:00
|
|
|
Living on the Edge
|
|
|
|
------------------
|
2010-04-19 23:03:08 +08:00
|
|
|
|
2010-06-26 14:39:16 +08:00
|
|
|
If you want to work with the latest version of Flask, there are two ways: you
|
2016-07-06 03:00:43 +08:00
|
|
|
can either let ``pip`` pull in the development version, or you can tell
|
2011-06-26 10:14:34 +08:00
|
|
|
it to operate on a git checkout. Either way, virtualenv is recommended.
|
2010-04-19 23:03:08 +08:00
|
|
|
|
2010-06-26 14:39:16 +08:00
|
|
|
Get the git checkout in a new virtualenv and run in development mode::
|
2010-04-19 23:03:08 +08:00
|
|
|
|
2017-02-11 17:43:11 +08:00
|
|
|
$ git clone https://github.com/pallets/flask.git
|
2010-04-19 23:03:08 +08:00
|
|
|
Initialized empty Git repository in ~/dev/flask/.git/
|
|
|
|
$ cd flask
|
2017-03-20 01:01:23 +08:00
|
|
|
$ virtualenv virtenv
|
|
|
|
New python executable in virtenv/bin/python
|
2014-02-11 04:24:31 +08:00
|
|
|
Installing setuptools, pip............done.
|
2017-03-20 01:01:23 +08:00
|
|
|
$ . virtenv/bin/activate
|
2010-04-19 23:03:08 +08:00
|
|
|
$ python setup.py develop
|
|
|
|
...
|
|
|
|
Finished processing dependencies for Flask
|
|
|
|
|
2010-06-26 14:39:16 +08:00
|
|
|
This will pull in the dependencies and activate the git head as the current
|
2011-06-26 10:14:34 +08:00
|
|
|
version inside the virtualenv. Then all you have to do is run ``git pull
|
|
|
|
origin`` to update to the latest version.
|
2010-04-19 23:03:08 +08:00
|
|
|
|
2010-04-11 08:20:10 +08:00
|
|
|
.. _windows-easy-install:
|
|
|
|
|
2014-02-11 04:24:31 +08:00
|
|
|
`pip` and `setuptools` on Windows
|
|
|
|
---------------------------------
|
|
|
|
|
2016-07-06 03:00:43 +08:00
|
|
|
Sometimes getting the standard "Python packaging tools" like ``pip``, ``setuptools``
|
|
|
|
and ``virtualenv`` can be a little trickier, but nothing very hard. The crucial
|
|
|
|
package you will need is pip - this will let you install
|
|
|
|
anything else (like virtualenv). Fortunately there is a "bootstrap script"
|
|
|
|
you can run to install.
|
2014-02-11 04:24:31 +08:00
|
|
|
|
2016-07-06 03:00:43 +08:00
|
|
|
If you don't currently have ``pip``, then `get-pip.py` will install it for you.
|
2014-02-11 04:24:31 +08:00
|
|
|
|
|
|
|
`get-pip.py`_
|
2010-04-11 08:20:10 +08:00
|
|
|
|
2016-07-06 03:00:43 +08:00
|
|
|
It should be double-clickable once you download it. If you already have ``pip``,
|
2014-02-11 04:24:31 +08:00
|
|
|
you can upgrade them by running::
|
2010-04-11 08:20:10 +08:00
|
|
|
|
2014-02-11 04:24:31 +08:00
|
|
|
> pip install --upgrade pip setuptools
|
2010-04-11 08:20:10 +08:00
|
|
|
|
2016-07-06 03:00:43 +08:00
|
|
|
Most often, once you pull up a command prompt you want to be able to type ``pip``
|
|
|
|
and ``python`` which will run those things, but this might not automatically happen
|
2014-02-11 04:24:31 +08:00
|
|
|
on Windows, because it doesn't know where those executables are (give either a try!).
|
2010-04-11 08:20:10 +08:00
|
|
|
|
2014-02-11 04:24:31 +08:00
|
|
|
To fix this, you should be able to navigate to your Python install directory
|
2016-03-14 23:02:29 +08:00
|
|
|
(e.g :file:`C:\Python27`), then go to :file:`Tools`, then :file:`Scripts`, then find the
|
2014-11-05 16:42:40 +08:00
|
|
|
:file:`win_add2path.py` file and run that. Open a **new** Command Prompt and
|
2016-07-06 03:00:43 +08:00
|
|
|
check that you can now just type ``python`` to bring up the interpreter.
|
2010-04-11 08:20:10 +08:00
|
|
|
|
2014-02-11 04:24:31 +08:00
|
|
|
Finally, to install `virtualenv`_, you can simply run::
|
2012-02-07 09:28:17 +08:00
|
|
|
|
2014-02-11 04:24:31 +08:00
|
|
|
> pip install virtualenv
|
2014-07-24 23:03:56 +08:00
|
|
|
|
2014-02-11 04:24:31 +08:00
|
|
|
Then you can be off on your way following the installation instructions above.
|
2012-02-07 09:28:17 +08:00
|
|
|
|
2016-03-23 18:27:05 +08:00
|
|
|
.. _get-pip.py: https://bootstrap.pypa.io/get-pip.py
|