All Linux distributions I have used have Python installed by default. The reason for this is that some parts of the Linux infrastructure rely on Python scripts - this is a very important point that will affect the way you set up your Flask development environment.
The first thing you will need to do is to check your Python version. I want to learn Python 3 as the transition from Python 2 to Python 3 is well on its way, but Flask doesn't support all Python 3 versions. So, to check your default Python version, open your terminal and type:
[anon@test ~]$ pythonThis starts the interactive Python shell with its prompt:
Python 3.6.0 (default, Jan 16 2017, 12:12:55) [GCC 6.3.1 20170109] on linux Type "help", "copyright", "credits" or "license" for more information. >>>I am using Antergos, an Arch based Linux distibution and my defalut Python is 3.6.0. If I type:
[anon@test ~]$ python2The prompt looks like this:
Python 2.7.13 (default, Dec 21 2016, 07:16:46) [GCC 6.2.1 20160830] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>so I also have Python 2.7.13 available. On Debian based distributions, like Ubuntu, Linux Mint and others the situation is reverse: The default Python version is 2 so if you type python you start Python 2.7.x and if you type Python3 you start Python 3.
The first thing to do is to install Flask. Flask is distributed as a Python package and you can install it from your terminal using Python's pip command. As it is mentioned in the tutorial you don't want to install Flask (or any other random Python package) in your system Python environment. Instead, you install it using virtualenv.
Virtualenv is a Python tool that lets you create a copy of a Python installation in a directory that you choose. This is very handy as it lets you experiment with using Python packages - when you mess something up you can just delete your virtual environment and start over, without affecting your main installation. Let's start.
When you open your terminal, its working directory is your home directory (/home/anon). You can list the contents of the current directory using:
[anon@test ~]$ lsls stands for list, i.e. list the contents of the current directory. The output looks like this:
... Desktop Documents Downloads ...You want to create your Flask project in your Documents directory, so type:
[anon@test ~]$ cd Documents [anon@test Documents]$ mkdir microblog [anon@test Documents]$ cd microblog [anon@test microblog]$cd stands for change directory and mkdir for make [new] directory. The result is that you created a directory named microblog in your Documents (/home/anon/Documents/microblog) and changed your working directory to it. Of course, at this point the microblog directory is still empty.
(Generally, it is a good thing when your Linux terminal doesn't respond to your commands. This means that all went well and there is nothing for the terminal to complain about.)
The next thing to do is to create your Python virtual environment. In the terminal , type:
[anon@test microblog]$ python -m venv flaskLet's break this command up. When invoked from the terminal Python can optionally accept a number of command line switches (or options) that affect its behaviour. If you type
[anon@test ~]$ python --helpPython lists all available command line switches. One of the switches is -m:
... -m mod : run library module as a script (terminates option list) ...So the command python -m mod[ule name] runs a Python module from the terminal. In our case the module name is venv, the module that lets you manage Python virtual environments.
The venv module itself also accepts command line arguments. You can list them by typing
[anon@test ~]$ python -m venv --helpThe output is:
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade] [--without-pip] [--prompt PROMPT] ENV_DIR [ENV_DIR ...] ... positional arguments: ENV_DIR A directory to create the environment in. ...Arguments enclosed in square brackets are optional. That is, the only argument that you have to pass to venv is ENV_DIR which specifies the directory in which your virtual environment will be created.
So, you can finally decipher the command that you used to create your virtual environment ([anon@test microblog]$ python -m venv flask): It invokes Python from the terminal to execute the venv module as a script and create a virtual environment in the specified directory (/home/anon/Documents/microblog/flask).
(I am lucky in that my version of Python - 3.6 - has the venv module installed by default. If you are running Python 2 you may need to use commands that are a little bit different than I listed above. The venv module is introduced in Python 3 and in Python 2 you will need to use virtualenv instead. You can find a few more details about this if you read this Stack Overflow thread.)
What dit the command actually do? It created a directory named flask in our microblog directory and copied your complete Python environment including the Python executable, pip and its site-packages. You can examine the flask directory contents by typing:
[anon@test microblog]$ find . -type f
The output is:
./flask/bin/easy_install ./flask/bin/easy_install-3.6 ./flask/bin/activate.csh ./flask/bin/pip3 ./flask/bin/pip ./flask/bin/pip3.6 ./flask/bin/activate.fish ./flask/bin/activate ... ... ... ./flask/lib/python3.6/site-packages/setuptools/archive_util.py ./flask/lib/python3.6/site-packages/setuptools/package_index.py ./flask/lib/python3.6/site-packages/setuptools/cli-64.exe ./flask/lib/python3.6/site-packages/setuptools/cli.exe ./flask/lib/python3.6/site-packages/setuptools/windows_support.py ./flask/lib/python3.6/site-packages/setuptools/namespaces.py ./flask/lib/python3.6/site-packages/setuptools/cli-32.exe ./flask/lib/python3.6/site-packages/setuptools/version.py ./flask/lib/python3.6/site-packages/setuptools/dist.py
find is another Linux command that you can use from the terminal. You can try to find out what it does by typing:
[anon@test microblog]$ find -helpbut the output is not very helpful. In Linux, though, there is another way to get help about commands. In your terminal type:
[anon@test microblog]$ man find.
The output is much more helpful:
NAME find - search for files in a directory hierarchy SYNOPSIS find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression] DESCRIPTION This manual page documents the GNU version of find. GNU find searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name. If no starting-point is specified, `.' is assumed. ... ... ...man stands for manual and it displays Linux manual pages. You can even use:
[anon@test microblog]$ man manto get help about the man[ual] command itself:
NAME man - an interface to the on-line reference manuals ... DESCRIPTION man is the system's manual pager. Each page argument given to man is normally the name of a program, utility or function. The manual page associated with each of these arguments is then found and displayed. A section, if provided, will direct man to look only in that section of the manual. The default action is to search in all of the available sections following a pre-defined order ("1 n l 8 3 0 2 5 4 9 6 7" by default, unless overridden by the SECTION directive in /etc/man_db.conf), and to show only the first page found, even if page exists in several sections. ... ...find searches for files in a directory hierarchy. In our case the root directory given to find is our current directory - microblog - specified by the dot (.) and we only list files as specifid by the -type f parameter.
The next thing you need to do is install several Python modules related to flask. Before that you need to activate the virtual environment you just created so that you newly installed modules don't pollute your system Python environment. In your terminal - making sure that you are still in the /home/anon/Documents/microblog directory - type:
[anon@test microblog]$ source flask/bin/activateIf all goes well your terminal prompt should look like this:
(flask) [anon@test microblog]$
Note the (flask) part that appeared at the beginning of the prompt - this means that you have successfully activated your virtual environment. From now on all your commands like pip imports will affect your flask virtual environment rather than your system Python environment, so keep an eye at that (flask) part. To confirm this, type this at your command prompt:
(flask) [anon@test microblog]$ which python
The which command displays the location of the command you passed to it as the argument, in this case python. The output of which should look like this:
/home/anon/Documents/microblog/flask/bin/python
So, from now on you will really be working with a local copy of the Python executable.
To deactivate your flask virtual environment, type:
(flask) [anon@test microblog]$ deactivate
and your prompt changes back to:
[anon@test microblog]$
Don't deactivate it just yet, as we are about to import flask related Python modules.
(You can learn more about venv reading its documentation.)
And now, finally, you can use pip to install flask modules. At your terminal type the following commands one at a time:
(flask) [anon@test microblog]$ pip install flask (flask) [anon@test microblog]$ pip install flask-login (flask) [anon@test microblog]$ pip install flask-openid (flask) [anon@test microblog]$ pip install flask-mail (flask) [anon@test microblog]$ pip install flask-sqlalchemy (flask) [anon@test microblog]$ pip install sqlalchemy-migrate (flask) [anon@test microblog]$ pip install flask-whooshalchemy (flask) [anon@test microblog]$ pip install flask-wtf (flask) [anon@test microblog]$ pip install flask-babel (flask) [anon@test microblog]$ pip install guess_language (flask) [anon@test microblog]$ pip install flipflop (flask) [anon@test microblog]$ pip install coverage
After each command pip will download the specified module with its dependencies and install it in your flask virtual environment. While doing this pip displays download and installation progress and any errors that may have ocurred during the installation. If all goes well you are set to go.
Well, that's about it, the tedious part of the tutorial is over and you can start having fun!
Nema komentara:
Objavi komentar