Skip to content
Myr

rewrite installation docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

discuss python version discuss all dependencies prefer python 3 in instructions [ci skip]

authored by David Lord · committed by David Lord · parent a80eab7 · Browse files · 1 file+108 added−123 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+108 added−123 removed

  • other:+108 added−123 removed

1 file+108 added−123 removed

modified:docs/installation.rst+108 added-123 removed
1 unmodified line
23Installation4============56Flask depends on some external libraries, like `Werkzeug7<http://werkzeug.pocoo.org/>`_ and `Jinja2 <http://jinja.pocoo.org/>`_.8Werkzeug is a toolkit for WSGI, the standard Python interface between web9applications and a variety of servers for both development and deployment.10Jinja2 renders templates.6Python Version7--------------812So how do you get all that on your computer quickly? There are many ways you13could do that, but the most kick-ass method is virtualenv, so let's have a look14at that first.9We recommend using the latest version of Python 3. Flask supports Python 3.310and newer, Python 2.6 and newer, and PyPy.1116You will need Python 2.6 or newer to get started, so be sure to have an17up-to-date Python 2.x installation. For using Flask with Python 3 have a18look at :ref:`python3-support`.12Dependencies13------------1420.. _virtualenv:15These distributions will be installed automatically when installing Flask.1622virtualenv23----------17* `Werkzeug`_ implements WSGI, the standard Python interface between18 applications and servers.19* `Jinja`_ is a template language that renders the pages your application20 serves.21* `MarkupSafe`_ comes with Jinja. It escapes untrusted input when rendering22 templates to avoid injection attacks.23* `ItsDangerous`_ securely signs data to ensure its integrity. This is used24 to protect Flask's session cookie.25* `Click`_ is a framework for writing command line applications. It provides26 the ``flask`` command and allows adding custom management commands.2725Virtualenv is probably what you want to use during development, and if you have26shell access to your production machines, you'll probably want to use it there,27too.28.. _Werkzeug: http://werkzeug.pocoo.org/29.. _Jinja: http://jinja.pocoo.org/30.. _MarkupSafe: https://pypi.python.org/pypi/MarkupSafe31.. _ItsDangerous: https://pythonhosted.org/itsdangerous/32.. _Click: http://click.pocoo.org/3329What problem does virtualenv solve? If you like Python as much as I do,30chances are you want to use it for other projects besides Flask-based web31applications. But the more projects you have, the more likely it is that you32will be working with different versions of Python itself, or at least different33versions of Python libraries. Let's face it: quite often libraries break34backwards compatibility, and it's unlikely that any serious application will35have zero dependencies. So what do you do if two or more of your projects have36conflicting dependencies?34Optional dependencies35~~~~~~~~~~~~~~~~~~~~~3638Virtualenv to the rescue! Virtualenv enables multiple side-by-side39installations of Python, one for each project. It doesn't actually install40separate copies of Python, but it does provide a clever way to keep different41project environments isolated. Let's see how virtualenv works.37These distributions will not be installed automatically. Flask will detect and38use them if you install them.3940* `Blinker`_ provides support for :ref:`signals`.41* `SimpleJSON`_ is a fast JSON implementation that is compatible with42 Python's ``json`` module. It is preferred for JSON operations if it is43 installed.4444.. admonition:: A note on python3 and virtualenv45.. _Blinker: https://pythonhosted.org/blinker/46.. _SimpleJSON: https://simplejson.readthedocs.io/4746 If you are planning on using python3 with the virtualenv, you don't need to47 install ``virtualenv``. Python3 has built-in support for virtual environments.48Virtual environments49--------------------5049If you are on Mac OS X or Linux, chances are that the following50command will work for you::51Use a virtual environment to manage the dependencies for your project, both in52development and in production.5352 $ sudo pip install virtualenv54What problem does a virtual environment solve? The more Python projects you55have, the more likely it is that you need to work with different versions of56Python libraries, or even Python itself. Newer versions of libraries for one57project can break compatibility in another project.5854It will probably install virtualenv on your system. Maybe it's even55in your package manager. If you use Ubuntu, try::59Virtual environments are independent groups of Python libraries, one for each60project. Packages installed for one project will not affect other projects or61the operating system's packages.6257 $ sudo apt-get install python-virtualenv63Python 3 comes bundled with the :mod:`venv` module to create virtual64environments. If you're using a modern version of Python, you can continue on65to the next section.6659If you are on Windows and don't have the ``easy_install`` command, you must60install it first. Check the :ref:`windows-easy-install` section for more61information about how to do that. Once you have it installed, run the same62commands as above, but without the ``sudo`` prefix.67If you're using Python 2, see :ref:`install-install-virtualenv` first.6864Creating a virtual environment65~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~69.. _install-create-env:7067Once you have virtualenv installed, just fire up a shell and create68your own environment. I usually create a project folder and a :file:`virtenv`69folder within::71Create an environment72~~~~~~~~~~~~~~~~~~~~~7371 $ mkdir myproject72 $ cd myproject74Create a project folder and a :file:`venv` folder within:7574There is a little change in how you create a virtualenv depending on which python-version you are currently using.76.. code-block:: sh7776**Python2**78 mkdir myproject79 cd myproject80 python3 -m venv venv8178::82On Windows:8380 $ virtualenv virtenv81 New python executable in virtenv/bin/python82 Installing setuptools, pip............done.84.. code-block:: bat8584**Python 3.6 and above**86 py -3 -m venv venv8786::88If you needed to install virtualenv because you are on an older version of89Python, use the following command instead:9088 $ python3 -m venv virtenv91.. code-block:: sh9290Activating a virtual environment91~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~93 virtualenv venv9493Now, whenever you want to work on a project, you only have to activate the94corresponding environment. On OS X and Linux, do the following::95On Windows:9696 $ . virtenv/bin/activate97.. code-block:: bat9898If you are a Windows user, the following command is for you::99 \Python27\Scripts\virtualenv.exe venv100100 $ virtenv\Scripts\activate101Activate the environment102~~~~~~~~~~~~~~~~~~~~~~~~103102Either way, you should now be using your virtualenv (notice how the prompt of103your shell has changed to show the active environment).104Before you work on your project, activate the corresponding environment:105105And if you want to go back to the real world, use the following command::106.. code-block:: sh107107 $ deactivate108 . venv/bin/activate109109After doing this, the prompt of your shell should be as familiar as before.110On Windows:111111Now, let's move on. Enter the following command to get Flask activated in your112virtualenv::112.. code-block:: bat113114 $ pip install Flask114 venv\Scripts\activate115116A few seconds later and you are good to go.116Your shell prompt will change to show the name of the activated environment.117118Install Flask119-------------120119System-Wide Installation120------------------------121Within the activated environment, use the following command to install Flask:122122This is possible as well, though I do not recommend it. Just run123``pip`` with root privileges::123.. code-block:: sh124125 $ sudo pip install Flask125 pip install Flask126127(On Windows systems, run it in a command-prompt window with administrator128privileges, and leave out ``sudo``.)127Living on the edge128~~~~~~~~~~~~~~~~~~129130If you want to work with the latest Flask code before it's released, install or131update the code from the master branch:132131Living on the Edge132------------------133.. code-block:: sh134134If you want to work with the latest version of Flask, there are two ways: you135can either let ``pip`` pull in the development version, or you can tell136it to operate on a git checkout. Either way, virtualenv is recommended.135 pip install -U https://github.com/pallets/flask/archive/master.tar.gz136138Get the git checkout in a new virtualenv and run in development mode::137.. _install-install-virtualenv:138140 $ git clone https://github.com/pallets/flask.git141 Initialized empty Git repository in ~/dev/flask/.git/142 $ cd flask143 $ virtualenv virtenv144 New python executable in virtenv/bin/python145 Installing setuptools, pip............done.146 $ . virtenv/bin/activate147 $ python setup.py develop148 ...149 Finished processing dependencies for Flask139Install virtualenv140------------------141151This will pull in the dependencies and activate the git head as the current152version inside the virtualenv. Then all you have to do is run ``git pull153origin`` to update to the latest version.142If you are using Python 2, the venv module is not available. Instead,143install `virtualenv`_.144155.. _windows-easy-install:145On Linux, virtualenv is provided by your package manager:146157`pip` and `setuptools` on Windows158---------------------------------147.. code-block:: sh148160Sometimes getting the standard "Python packaging tools" like ``pip``, ``setuptools``161and ``virtualenv`` can be a little trickier, but nothing very hard. The crucial162package you will need is pip - this will let you install163anything else (like virtualenv). Fortunately there is a "bootstrap script"164you can run to install.149 # Debian, Ubuntu150 sudo apt-get install python-virtualenv151166If you don't currently have ``pip``, then `get-pip.py` will install it for you.152 # CentOS, Fedora153 sudo yum install python-virtualenv154168`get-pip.py`_155 # Arch156 sudo pacman -S python-virtualenv157170It should be double-clickable once you download it. If you already have ``pip``,171you can upgrade them by running::158If you are on Mac OS X or Windows, download `get-pip.py`_, then:159173 > pip install --upgrade pip setuptools160.. code-block:: sh161175Most often, once you pull up a command prompt you want to be able to type ``pip``176and ``python`` which will run those things, but this might not automatically happen177on Windows, because it doesn't know where those executables are (give either a try!).162 sudo python2 Downloads/get-pip.py163 sudo python2 -m pip install virtualenv164179To fix this, you should be able to navigate to your Python install directory180(e.g :file:`C:\Python27`), then go to :file:`Tools`, then :file:`Scripts`, then find the181:file:`win_add2path.py` file and run that. Open a **new** Command Prompt and182check that you can now just type ``python`` to bring up the interpreter.165On Windows, as an administrator:166184Finally, to install `virtualenv`_, you can simply run::167.. code-block:: bat168186 > pip install virtualenv169 \Python27\python.exe Downloads\get-pip.py170 \Python27\python.exe -m pip install virtualenv171188Then you can be off on your way following the installation instructions above.172Now you can continue to :ref:`install-create-env`.173174.. _virtualenv: https://virtualenv.pypa.io/175.. _get-pip.py: https://bootstrap.pypa.io/get-pip.py